diff --git a/src/bin/dctquery.rs b/src/bin/dctquery.rs index 924b6d0..5cd2a84 100644 --- a/src/bin/dctquery.rs +++ b/src/bin/dctquery.rs @@ -7,33 +7,33 @@ use clap::Parser; #[derive(Parser)] #[command(version, about, long_about = None)] struct Cfg { - /// Path to the image - path: String, - /// Try to output the images to terminal with viuer - #[arg(short, long, default_value_t=false)] - show_images: bool, - distance: isize, + /// Path to the image + path: String, + /// Try to output the images to terminal with viuer + #[arg(short, long, default_value_t=false)] + show_images: bool, + distance: isize, } fn main() { - env_logger::init(); + env_logger::init(); - let desc = DCT::new().with_quality(50); + let desc = DCT::new().with_quality(50); - let cfg = Cfg::parse(); + let cfg = Cfg::parse(); - let img = image::open(cfg.path) - .expect("Unable to open file"); - // let conf = viuer::Config { - // width: Some(16), - // height: Some(8), - // ..Default::default() - // }; - // viuer::print(&img, &conf).expect("Image printing failed."); - let store = - DescriptorStore::new() - .with_file("dct50.messagepack"); - let phash: u64 = desc.describe(&img); + let img = image::open(cfg.path) + .expect("Unable to open file"); + // let conf = viuer::Config { + // width: Some(16), + // height: Some(8), + // ..Default::default() + // }; + // viuer::print(&img, &conf).expect("Image printing failed."); + let phash: u64 = desc.describe(&img); + let store = + DescriptorStore::new(desc) + .with_file("dct50.messagepack"); info!("Phash integer:\n{phash}"); info!("Phash binary:\n{phash:064b}"); store.print_nn(phash, cfg.distance, cfg.show_images); diff --git a/src/main.rs b/src/main.rs index e8fc6e9..c1317ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,12 +12,12 @@ struct Cfg { path: PathBuf } -fn make_store(descriptor: T, input_path: PathBuf, save_path: PathBuf) -> DescriptorStore { +fn make_store(descriptor: T, input_path: PathBuf, save_path: PathBuf) -> DescriptorStore { let mut store = - DescriptorStore::new() + DescriptorStore::new(descriptor) .with_file(save_path); info!("{}", store); - store.insert_directory(input_path, descriptor); + store.insert_directory(input_path); info!("{}", store); store.save().expect("Error saving"); store diff --git a/src/store.rs b/src/store.rs index c31a90d..231c7b3 100644 --- a/src/store.rs +++ b/src/store.rs @@ -17,7 +17,8 @@ pub enum SaveError { /// Uses a hashmap to map descriptors to buckets of files. /// Also keeps a BK-tree for quick distance ranking -pub struct DescriptorStore { +pub struct DescriptorStore { + descriptor: T, /// Main hashmap that maps descriptors to buckets of filenames map: HashMap>, @@ -31,14 +32,15 @@ pub struct DescriptorStore { seen: Option>, } -impl DescriptorStore { +impl DescriptorStore { /// Makes a new empty DescriptorStore with default settings - pub fn new() -> Self { + pub fn new(descriptor: T) -> Self { let map: HashMap> = HashMap::new(); //let seen: HashSet = HashSet::new(); //let seen = None; DescriptorStore { map, + descriptor, save_location: std::path::PathBuf::from("store.messagepack"), bktree: BkTree::new(hamming_distance), seen: None, @@ -119,7 +121,7 @@ impl DescriptorStore { } /// Calculates all descriptions with a given descriptor - pub fn insert_directory>(&mut self, dir: U, desc: T) { + pub fn insert_directory>(&mut self, dir: U) { for node in fs::read_dir(dir).unwrap() { let file = node.expect("Error walking directory"); let name = match file.file_name().into_string() { @@ -138,7 +140,7 @@ impl DescriptorStore { continue } }; - let phash = desc.describe(&img); + let phash = self.descriptor.describe(&img); if self.contains(phash) { println!("{} duplicate of {:?}", name, self.map.get(&phash)); } @@ -222,7 +224,7 @@ impl DescriptorStore { } } -impl fmt::Display for DescriptorStore { +impl fmt::Display for DescriptorStore { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let mut output = String::new(); for (key, bucket) in self.map.iter() {