You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.6 KiB

  1. // Copyright 2015 Drew Short <drew@sothr.com>.
  2. //
  3. // Licensed under the MIT license<LICENSE-MIT or http://opensource.org/licenses/MIT>.
  4. // This file may not be copied, modified, or distributed except according to those terms.
  5. extern crate pihash;
  6. extern crate rustc_serialize;
  7. extern crate docopt;
  8. use std::path::Path;
  9. use docopt::Docopt;
  10. // The usage description
  11. const USAGE: &'static str = "
  12. Perceptual Image Hashing (pihash)
  13. Usage:
  14. pihash [options] \
  15. <path>...
  16. pihash (--help | --version)
  17. Options:
  18. -h, --help \
  19. Show this screen.
  20. -V, --version Print version.
  21. -a, \
  22. --ahash Include an ahash calculation.
  23. -d, --dhash \
  24. Include an dhash calculation.
  25. -p, --phash Include an phash \
  26. calculation.
  27. ";
  28. #[derive(Debug, RustcDecodable)]
  29. struct Args {
  30. flag_ahash: bool,
  31. flag_dhash: bool,
  32. flag_phash: bool,
  33. arg_path: Vec<String>,
  34. }
  35. fn main() {
  36. let args: Args = Docopt::new(USAGE)
  37. .and_then(|d| d.decode())
  38. .unwrap_or_else(|e| e.exit());
  39. // Init the hashing library
  40. pihash::init();
  41. // println!("{:?}", args);
  42. // All flags set or, no flags set
  43. if (args.flag_ahash && args.flag_dhash && args.flag_phash) ||
  44. (!args.flag_ahash && !args.flag_dhash && !args.flag_phash) {
  45. for path in args.arg_path {
  46. let image_path = Path::new(&path);
  47. let hashes = pihash::get_phashes(&image_path);
  48. let hash_result = format!(r#"
  49. file: {}
  50. ahash: {}
  51. dhash: {}
  52. phash: {}
  53. "#,
  54. hashes.orig_path,
  55. hashes.ahash,
  56. hashes.dhash,
  57. hashes.phash);
  58. println!("{}", hash_result);
  59. }
  60. // Otherwise process only specific hashes
  61. } else {
  62. for path in args.arg_path {
  63. println!("file: {}", path);
  64. let image_path = Path::new(&path);
  65. if args.flag_ahash {
  66. let ahash = pihash::get_ahash(&image_path);
  67. println!("ahash: {}", ahash);
  68. }
  69. if args.flag_dhash {
  70. let dhash = pihash::get_dhash(&image_path);
  71. println!("dhash: {}", dhash);
  72. }
  73. if args.flag_phash {
  74. let phash = pihash::get_phash(&image_path);
  75. println!("phash: {}", phash);
  76. }
  77. println!("");
  78. }
  79. }
  80. }