Skip to content

Instantly share code, notes, and snippets.

@flo-l
Last active September 30, 2015 15:38
Show Gist options
  • Save flo-l/64e59d8d19b50df26f21 to your computer and use it in GitHub Desktop.
Save flo-l/64e59d8d19b50df26f21 to your computer and use it in GitHub Desktop.
#![feature(result_expect)]
#[macro_use]
extern crate clap;
extern crate image;
extern crate rustc_serialize;
use rustc_serialize::base64::ToBase64;
use rustc_serialize::hex::ToHex;
fn main() {
// code that checks if files exist
let file_exists = |path| {
if std::fs::metadata(&path).is_ok() {
Ok(())
} else {
Err(String::from(format!("File '{}' doesn't exist", path)))
}
};
let matches = clap_app!(myapp =>
(version: "0.1")
(author: "Flo L <lacknerflo@gmail.com>")
(about: "Computes comparable hashes for given image files")
(@arg INPUT: ... +required {file_exists} "Sets the input file(s) to use")
).get_matches();
for p in matches.values_of("INPUT").unwrap().iter() {
do_the_work(p);
}
}
fn do_the_work(path: &str) {
// get to image
let image = image::open(path)
.expect("File not found..");
// prepare it
let simple = image
.grayscale()
.resize_exact(9, 8, image::FilterType::Nearest);
// the hash (64 bit for now)
let mut hash = vec![0u8; 8]; // vec of u8 representing the bits and bytes of the hash computed for the image.
let raw = simple.raw_pixels(); // vec of u8 representing gray pixels of an 9x8 image.
for x in 0..8 {
for y in 0..8 {
if raw[x*8+y] > raw[x*8+y+1] {
hash[x] |= 0x1 << y
}
}
}
for x in hash.iter() {
print!("{:08b}", x);
}
println!("");
//println!("{} {}", hash.to_hex(), path);
//println!("{} {}", hash.to_base64(rustc_serialize::base64::STANDARD), path);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment