Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use GtkScale instead radio buttons for similarity #397

Merged
merged 2 commits into from
Jul 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 32 additions & 42 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions czkawka_cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,15 @@ fn parse_delete_method(src: &str) -> Result<DeleteMethod, &'static str> {
}
}

// TODO For now it looks different
fn parse_similar_images_similarity(src: &str) -> Result<Similarity, &'static str> {
match src.to_ascii_lowercase().replace('_', "").as_str() {
"minimal" => Ok(Similarity::Minimal),
"verysmall" => Ok(Similarity::VerySmall),
"small" => Ok(Similarity::Small),
"medium" => Ok(Similarity::Medium),
"high" => Ok(Similarity::High),
"veryhigh" => Ok(Similarity::VeryHigh),
"minimal" => Ok(Similarity::Similar(5)),
"verysmall" => Ok(Similarity::Similar(4)),
"small" => Ok(Similarity::Similar(3)),
"medium" => Ok(Similarity::Similar(2)),
"high" => Ok(Similarity::Similar(1)),
"veryhigh" => Ok(Similarity::Similar(0)),
_ => Err("Couldn't parse the delete method (allowed: verysmall, small, medium, high, veryhigh)"),
}
}
Expand Down
2 changes: 1 addition & 1 deletion czkawka_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ directories-next = "2.0.0"

# Needed by similar images
img_hash = "3.2.0"
bk-tree = "0.4.0"
bk-tree = "0.3.0" # Do not update. Updating broke of searching for similar hashes
image = "0.23.14"
hamming = "0.1.3"

Expand Down
84 changes: 41 additions & 43 deletions czkawka_core/src/similar_images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@ pub struct ProgressData {
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
pub enum Similarity {
None,
Minimal,
VerySmall,
Small,
Medium,
High,
VeryHigh,
Similar(u64),
}

#[derive(Clone, Debug)]
Expand All @@ -60,14 +55,14 @@ pub struct FileEntry {
struct Hamming;

impl bk_tree::Metric<Node> for Hamming {
fn distance(&self, a: &Node, b: &Node) -> u32 {
hamming::distance_fast(a, b).unwrap() as u32
fn distance(&self, a: &Node, b: &Node) -> u64 {
hamming::distance_fast(a, b).unwrap() as u64
}

// TODO Probably needs to be implemented
fn threshold_distance(&self, _a: &Node, _b: &Node, _threshold: u32) -> Option<u32> {
None
}
// // TODO Probably needs to be implemented
// fn threshold_distance(&self, _a: &Node, _b: &Node, _threshold: u32) -> Option<u32> {
// None
// }
}

/// Struct to store most basics info about all folder
Expand Down Expand Up @@ -115,7 +110,7 @@ impl SimilarImages {
minimal_file_size: 1024 * 16, // 16 KB should be enough to exclude too small images from search
image_hashes: Default::default(),
stopped_search: false,
similarity: Similarity::High,
similarity: Similarity::Similar(1),
images_to_check: Default::default(),
use_cache: true,
}
Expand Down Expand Up @@ -416,6 +411,10 @@ impl SimilarImages {
let hash = hasher.hash_image(&image);
let mut buf = [0u8; 8];
buf.copy_from_slice(&hash.as_bytes());
if buf.iter().all(|e| *e == 0) {
// A little broken image
return Some(None);
}
file_entry.hash = buf;

Some(Some((file_entry, buf)))
Expand Down Expand Up @@ -455,14 +454,9 @@ impl SimilarImages {
Common::print_time(hash_map_modification, SystemTime::now(), "sort_images - saving data to files".to_string());
let hash_map_modification = SystemTime::now();

let similarity: u32 = match self.similarity {
Similarity::VeryHigh => 0,
Similarity::High => 1,
Similarity::Medium => 2,
Similarity::Small => 3,
Similarity::VerySmall => 4,
Similarity::Minimal => 5,
_ => panic!("0-5 similarity levels are allowed, check if not added more."),
let similarity: u64 = match self.similarity {
Similarity::Similar(k) => k,
_ => panic!(),
};

// TODO
Expand Down Expand Up @@ -500,7 +494,7 @@ impl SimilarImages {
dimensions: fe.dimensions.clone(),
modified_date: fe.modified_date,
hash: fe.hash,
similarity: Similarity::VeryHigh,
similarity: Similarity::Similar(0),
})
.collect();

Expand All @@ -522,15 +516,7 @@ impl SimilarImages {
dimensions: fe.dimensions.clone(),
modified_date: fe.modified_date,
hash: [0; 8],
similarity: match similarity {
0 => Similarity::VeryHigh,
1 => Similarity::High,
2 => Similarity::Medium,
3 => Similarity::Small,
4 => Similarity::VerySmall,
5 => Similarity::Minimal,
_ => panic!("0-5 similarity levels are allowed, check if not added more."),
},
similarity: Similarity::Similar(*similarity),
})
.collect::<Vec<_>>()),
);
Expand Down Expand Up @@ -664,18 +650,6 @@ impl PrintResults for SimilarImages {
}
}

fn get_string_from_similarity(similarity: &Similarity) -> &str {
match similarity {
Similarity::Minimal => "Minimal",
Similarity::VerySmall => "Very Small",
Similarity::Small => "Small",
Similarity::Medium => "Medium",
Similarity::High => "High",
Similarity::VeryHigh => "Very High",
Similarity::None => panic!(),
}
}

fn save_hashes_to_file(hashmap: &BTreeMap<String, FileEntry>, text_messages: &mut Messages) {
if let Some(proj_dirs) = ProjectDirs::from("pl", "Qarmin", "Czkawka") {
// Lin: /home/username/.cache/czkawka
Expand Down Expand Up @@ -810,3 +784,27 @@ fn load_hashes_from_file(text_messages: &mut Messages) -> Option<BTreeMap<String
text_messages.messages.push("Cannot find or open system config dir to save cache file".to_string());
None
}
pub fn get_string_from_similarity(similarity: &Similarity) -> String {
match similarity {
Similarity::None => {
panic!()
}
Similarity::Similar(k) => {
if *k < 1 {
format!("Very High {}", *k)
} else if *k < 2 {
format!("High {}", *k)
} else if *k < 4 {
format!("Medium {}", *k)
} else if *k < 6 {
format!("Small {}", *k)
} else if *k < 9 {
format!("Very Small {}", *k)
} else if *k < 13 {
format!("Minimal {}", *k)
} else {
panic!()
}
}
}
}
Loading