Skip to content

Instantly share code, notes, and snippets.

@stevenblenkinsop
Forked from anonymous/playground.rs
Last active October 1, 2015 02:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenblenkinsop/04d6c84f17e14d2d5de8 to your computer and use it in GitHub Desktop.
Save stevenblenkinsop/04d6c84f17e14d2d5de8 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
fn old() -> Vec<u8> {
let mut hash = vec![0u8; 8]; // vec of u8 representing the bits and bytes of the hash computed for the image.
let raw: Vec<_> = (0..8).flat_map(|_| (0 .. 9).rev()).collect(); // vec of u8 representing gray pixels of an 9x8 image.
for x in 0..8 {
for y in 0..8 {
if raw[x*9+y] > raw[x*9+y+1] {
hash[x] |= 0x1 << y
}
}
}
hash
}
fn new() -> Vec<u8> {
let mut hash = vec![0u8; 8]; // vec of u8 representing the bits and bytes of the hash computed for the image.
let raw: Vec<_> = (0..8).flat_map(|_| (0 .. 9).rev()).collect(); // vec of u8 representing gray pixels of an 9x8 image.
for (row, hash_byte) in raw.chunks(9).zip(&mut hash) {
*hash_byte = row.windows(2).enumerate().filter_map(|(i, window)| {
if window[0] > window[1] { Some(i) } else { None }
}).fold(0, |accum, i| accum | 0x1 << i);
}
hash
}
fn main(){
println!("old: {:?}", old());
println!("new: {:?}", new());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment