In which Rust and perceptual hashing come together.
Created
February 5, 2018 10:32
-
-
Save jstepien/d38f84a01feedbc8d3861185ad6a7f11 to your computer and use it in GitHub Desktop.
That looks oddly familiar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "phash" | |
version = "0.1.0" | |
authors = ["Jan Stępień"] | |
[dependencies] | |
libc = "0.2.36" | |
rayon = "0.9.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate libc; | |
extern crate rayon; | |
use std::env; | |
use std::ffi::CString; | |
use libc::{uint64_t, c_char, c_int}; | |
use rayon::prelude::*; | |
#[link(name = "pHash")] | |
extern { | |
fn ph_dct_imagehash( | |
file: *const c_char, | |
hash: *mut uint64_t | |
) -> c_int; | |
} | |
fn imagehash(file: &str) -> Option<u64> { | |
let mut hash: u64 = 0; | |
let cstr = CString::new(file).unwrap(); | |
unsafe { | |
let ptr = cstr.as_ptr(); | |
match ph_dct_imagehash(ptr, &mut hash) { | |
0 => Some(hash), | |
_ => None, | |
} | |
} | |
} | |
fn main() { | |
let files: Vec<_> = env::args().skip(1).collect(); | |
files.par_iter().for_each(|file| { | |
match imagehash(&file) { | |
Some(hash) => println!("{:016x} {}", hash, file), | |
None => eprintln!("Failed to hash {}", file), | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment