photodna
Safe, high-level Rust bindings for the Microsoft PhotoDNA Edge Hash Generator.
Overview
PhotoDNA is a perceptual hashing technology that creates a compact "fingerprint" of an image. This fingerprint can be used to identify visually similar images even after modifications like resizing, cropping, color adjustments, or format conversion.
This crate provides a safe, ergonomic Rust API on top of the low-level photodna-sys bindings.
Features
- Safe API – All unsafe FFI operations are encapsulated behind a safe interface
- Zero-Copy Hashes – The
Hashtype uses a fixed-size stack array (no heap allocation) - Typed Errors – Comprehensive error handling via
PhotoDnaError - Builder Pattern – Ergonomic configuration via
GeneratorOptionsandHashOptions
Requirements
This crate requires the proprietary Microsoft PhotoDNA SDK.
Before building, you must set the PHOTODNA_SDK_ROOT environment variable to point to the SDK installation directory:
See the photodna-sys crate documentation for detailed setup instructions.
Quick Start
use ;
API Overview
Generator
The main entry point. Manages the PhotoDNA library lifecycle:
use ;
let generator = new?;
// Check library version
println!;
Hash
A fixed-size (924 bytes) perceptual hash with zero-copy semantics:
use Hash;
// Format as hex for storage
let hex: String = hash.to_hex;
// Parse from hex
let hash = from_hex.unwrap;
// Access raw bytes
let bytes: & = hash.as_bytes;
// Use as HashMap key (implements Hash trait)
use HashSet;
let mut seen = new;
seen.insert;
Pixel Formats
Multiple pixel formats are supported:
use ;
let options = new
.pixel_format // Common in Windows
.remove_border; // Auto-detect and remove borders
Supported formats:
Rgb,Bgr(3 bytes/pixel)Rgba,Bgra,Argb,Abgr(4 bytes/pixel)Gray8(1 byte/pixel),Gray32(4 bytes/pixel)Cmyk,YCbCr,Yuv420p
Border Detection
Automatically detect and remove borders from images:
let result = generator.compute_hash_with_border_detection?;
println!;
if let Some = result.borderless
Error Handling
All operations return Result<T, PhotoDnaError>:
use ;
Thread Safety
Generator implements Send but not Sync. Options for concurrent use:
// Option 1: One generator per thread
let generator = new?;
// Option 2: Shared with Mutex
use ;
let generator = new;
// Option 3: Use max_threads for internal parallelism
let generator = new?;
// Internal operations can use up to 8 threads
Platform Support
| Platform | Architecture | Support |
|---|---|---|
| Windows | x86_64, x86, arm64 | ✅ Native library |
| Linux | x86_64, x86, arm64 | ✅ Native library |
| macOS | x86_64, arm64 | ✅ Native library |
| BSD | any | 🔧 WebAssembly (via photodna-sys) |
License
This crate is licensed under MIT OR Apache-2.0.
Note: The PhotoDNA library itself is proprietary software from Microsoft. Usage of PhotoDNA requires a separate license agreement with Microsoft.