Expand description
§photodna-sys
Low-level, unsafe FFI bindings to the Microsoft PhotoDNA Edge Hash Generator library.
This crate provides raw bindings to the proprietary Microsoft PhotoDNA library, which computes perceptual hashes of images for content identification purposes.
§Library Loading Model
The PhotoDNA library is designed for runtime dynamic loading via dlopen/LoadLibrary,
not compile-time linking. This crate provides:
- Type definitions and constants compatible with the C API
- Function pointer types for all library functions
- The
EdgeHashGeneratorstruct that handles library loading and provides safe function access
§Requirements
This crate requires the proprietary Microsoft PhotoDNA SDK. You must set the
PHOTODNA_SDK_ROOT environment variable to point to the SDK installation directory
before building.
export PHOTODNA_SDK_ROOT=/path/to/PhotoDNA.EdgeHashGeneration-1.05.001§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 module (requires wasm feature) |
§Features
native(default): Enables runtime loading of native dynamic libraries (.dll/.so).wasm: Embeds the WebAssembly module for platforms without native library support.bindgen: Regenerates bindings from C headers at build time (requires clang).
§Safety
All FFI functions in this crate are unsafe. Callers must ensure:
- The library has been properly initialized via
EdgeHashGenerator::new. - All pointers passed to functions are valid and point to sufficient memory.
- Image data buffers match the specified dimensions, stride, and pixel format.
- The library instance is not used after being dropped.
§Example
ⓘ
use photodna_sys::*;
// Initialize the library
let lib = EdgeHashGenerator::new(None, 4)?; // Use default path, 4 threads
// Prepare image data (RGB format)
let image_data: &[u8] = /* your image pixels */;
let width = 100;
let height = 100;
// Compute the hash
let mut hash = [0u8; PHOTODNA_HASH_SIZE_MAX];
unsafe {
let result = lib.photo_dna_edge_hash(
image_data.as_ptr(),
hash.as_mut_ptr(),
width,
height,
0, // auto-calculate stride
PhotoDna_Default,
);
if result < 0 {
eprintln!("Error: {}", error_code_description(result));
}
}Structs§
- Edge
Hash Generator - The PhotoDNA Edge Hash Generator library wrapper.
- Hash
Result - Result structure returned by border detection hash functions.
Constants§
- PHOTODNA_
HASH_ SIZE_ EDGE_ V2 - Size of PhotoDNA Edge V2 hash in bytes (binary format).
- PHOTODNA_
HASH_ SIZE_ EDGE_ V2_ BASE64 - Size of PhotoDNA Edge V2 hash in bytes (Base64 format).
- PHOTODNA_
HASH_ SIZE_ MAX - Maximum hash buffer size required.
- PHOTODNA_
LIBRARY_ VERSION - Library version string.
- Photo
Dna_ Abgr - Pixel layout: ABGR, 4 bytes per pixel.
- Photo
Dna_ Argb - Pixel layout: ARGB, 4 bytes per pixel.
- Photo
Dna_ Bgr - Pixel layout: BGR, 3 bytes per pixel.
- Photo
Dna_ Bgra - Pixel layout: BGRA, 4 bytes per pixel.
- Photo
Dna_ Check Memory - Check data pointers for valid allocated memory. Note: This may negatively impact performance.
- Photo
Dna_ Cmyk - Pixel layout: CMYK, 4 bytes per pixel.
- Photo
Dna_ Default - Default options. The matcher will return all results found.
- Photo
Dna_ Edge V2 - Edge V2 format hash size.
- Photo
Dna_ Edge V2Base64 - Edge V2 format hash size (Base64 encoded).
- Photo
Dna_ Error BadArgument - An invalid argument was passed to the function.
- Photo
Dna_ Error Hash Format Invalid Characters - An invalid character was contained in a Base64 or Hex hash.
- Photo
Dna_ Error Host Memory Allocation Failed - Alias for memory allocation failure (host-side).
- Photo
Dna_ Error Image IsFlat - The image has few or no gradients.
- Photo
Dna_ Error Image TooSmall - Provided image had a dimension less than 50 pixels.
- Photo
Dna_ Error Invalid Hash - Hash that does not conform to PhotoDNA specifications.
- Photo
Dna_ Error Invalid Stride - Stride should be 0, or greater than or equal to width in bytes.
- Photo
Dna_ Error Invalid SubImage - The sub region area is not within the boundaries of the image.
- Photo
Dna_ Error Library Failure - General failure within the library.
- Photo
Dna_ Error Memory Access - System memory exception occurred.
- Photo
Dna_ Error Memory Allocation Failed - Failed to allocate memory.
- Photo
Dna_ Error NoBorder - A border was not detected for the image.
- Photo
Dna_ Error NoBorder Image TooSmall - Provided image had a dimension less than 50 pixels (no border variant).
- Photo
Dna_ Error Source Format Unknown - Not a known source image format.
- Photo
Dna_ Error Unknown - An undetermined error occurred.
- Photo
Dna_ Grey8 - Pixel layout: Grayscale 8-bit, 1 byte per pixel.
- Photo
Dna_ Grey32 - Pixel layout: Grayscale 32-bit, 4 bytes per pixel.
- Photo
Dna_ Hash Format Edge V2 - Hash output format: PhotoDNA Edge Hash V2.
- Photo
Dna_ Hash Format Edge V2Base64 - Hash output format: PhotoDNA Edge Hash V2 Base64.
- Photo
Dna_ Hash Format Mask - Mask to isolate the hash format bits.
- Photo
Dna_ MaxSize - Maximum hash size.
- Photo
Dna_ NoRotate Flip - Prevent checks for rotated and/or flipped orientations.
- Photo
Dna_ Option None - No options specified. See description for default behavior.
- Photo
Dna_ Other - Use same options as specified for primary parameter.
- Photo
Dna_ Pixel Layout Mask - Mask to isolate the pixel layout bits.
- Photo
Dna_ Remove Border - Check for and remove borders from the image.
- Photo
Dna_ Rgb - Pixel layout: RGB, 3 bytes per pixel.
- Photo
Dna_ Rgba - Pixel layout: RGBA, 4 bytes per pixel.
- Photo
Dna_ Rgba Pm - Pixel layout: RGBA with pre-multiplied alpha, 4 bytes per pixel.
- Photo
Dna_ Test - Equivalent to Verbose + CheckMemory.
- Photo
Dna_ Verbose - Enable debug output to stderr.
- Photo
Dna_ YCbCr - Pixel layout: YCbCr, 3 bytes per pixel.
- Photo
Dna_ Yuv420p - Pixel layout: YUV420P planar format. Y = 1 byte per pixel, U = 1 byte per 4 pixels, V = 1 byte per 4 pixels.
Functions§
- error_
code_ description - Returns a human-readable description of a PhotoDNA error code.
- get_
library_ filename - Returns the platform-specific library filename.
- hash_
size_ for_ options - Returns the expected hash size for the given options.
Type Aliases§
- Error
Code - Type alias for PhotoDNA error codes.
- FnEdge
Hash Generator Init - Function pointer type for EdgeHashGeneratorInit.
- FnEdge
Hash Generator Release - Function pointer type for EdgeHashGeneratorRelease.
- FnGet
Error Number - Function pointer type for GetErrorNumber.
- FnGet
Error String - Function pointer type for GetErrorString.
- FnLibrary
Version - Function pointer type for LibraryVersion.
- FnLibrary
Version Major - Function pointer type for LibraryVersionMajor.
- FnLibrary
Version Minor - Function pointer type for LibraryVersionMinor.
- FnLibrary
Version Patch - Function pointer type for LibraryVersionPatch.
- FnLibrary
Version Text - Function pointer type for LibraryVersionText.
- FnPhoto
DnaEdge Hash - Function pointer type for PhotoDnaEdgeHash.
- FnPhoto
DnaEdge Hash Border - Function pointer type for PhotoDnaEdgeHashBorder.
- FnPhoto
DnaEdge Hash Border Sub - Function pointer type for PhotoDnaEdgeHashBorderSub.
- FnPhoto
DnaEdge Hash Sub - Function pointer type for PhotoDnaEdgeHashSub.
- Hash
Size - Type alias for hash size identifiers.
- Photo
DnaOptions - Type alias for PhotoDNA option flags.