Skip to main content

squint_cli/
squint-cli.rs

1//! squint-cli — compute a perceptual image hash and print the hex string to stdout.
2//!
3//! Used by tools/cross-squint-diff for live cross-port equivalence checking.
4//!
5//! Usage: squint-cli <algo> <size> <path>
6//!   algo  — one of: phash, phash_simple, dhash, dhash_vertical, average_hash,
7//!           whash_haar, whash_db4, whash_db4_robust, colorhash, crop_resistant_hash
8//!   size  — hash_size (or binbits for colorhash); pass "-" for crop_resistant_hash
9//!   path  — path to the image file
10//!
11//! Exit:  0 on success, 1 on hash/decode error, 2 on usage error.
12
13use std::env;
14use std::process::ExitCode;
15
16use rosetta_squint::{
17    average_hash, colorhash, crop_resistant_hash, dhash, dhash_vertical, phash, phash_simple,
18    whash_db4, whash_db4_robust, whash_haar,
19};
20
21fn main() -> ExitCode {
22    let args: Vec<String> = env::args().collect();
23    if args.len() != 4 {
24        eprintln!("usage: squint-cli <algo> <size> <path>");
25        return ExitCode::from(2);
26    }
27    let algo = args[1].as_str();
28    let size: usize = args[2].parse().unwrap_or(8);
29    let path = &args[3];
30
31    let result = match algo {
32        "phash" => phash(path, size).map(|h| h.to_hex()),
33        "phash_simple" => phash_simple(path, size).map(|h| h.to_hex()),
34        "dhash" => dhash(path, size).map(|h| h.to_hex()),
35        "dhash_vertical" => dhash_vertical(path, size).map(|h| h.to_hex()),
36        "average_hash" => average_hash(path, size).map(|h| h.to_hex()),
37        "whash_haar" => whash_haar(path, size).map(|h| h.to_hex()),
38        "whash_db4" => whash_db4(path, size).map(|h| h.to_hex()),
39        "whash_db4_robust" => whash_db4_robust(path, size).map(|h| h.to_hex()),
40        "colorhash" => colorhash(path, size).map(|h| h.to_hex()),
41        "crop_resistant_hash" => crop_resistant_hash(path, None).map(|mh| mh.to_hex()),
42        _ => {
43            eprintln!("unknown algo: {}", algo);
44            return ExitCode::from(2);
45        }
46    };
47
48    match result {
49        Ok(hex) => {
50            println!("{}", hex);
51            ExitCode::from(0)
52        }
53        Err(e) => {
54            eprintln!("error: {}", e);
55            ExitCode::from(1)
56        }
57    }
58}