pub struct Generator { /* private fields */ }Expand description
The PhotoDNA hash generator.
This struct manages the underlying PhotoDNA library instance and provides safe methods for computing perceptual hashes.
§Thread Safety
The generator is Send but not Sync. To use from multiple threads,
either:
- Create one
Generatorper thread, or - Wrap in
Arc<Mutex<Generator>>for shared access
§Examples
use photodna::{Generator, GeneratorOptions};
// Create a generator with default options
let generator = Generator::new(GeneratorOptions::default())?;
// Check library version
if let Some(version) = generator.library_version_text() {
println!("PhotoDNA library version: {}", version);
}
// Compute a hash
let hash = generator.compute_hash_rgb(&image_data, 640, 480)?;Implementations§
Source§impl Generator
impl Generator
Sourcepub fn new(options: GeneratorOptions) -> Result<Self>
pub fn new(options: GeneratorOptions) -> Result<Self>
Creates a new PhotoDNA generator with the given options.
This loads the PhotoDNA library and initializes the internal state.
§Arguments
options- Configuration options for the generator.
§Errors
Returns an error if the library cannot be loaded or initialized.
§Examples
use photodna::{Generator, GeneratorOptions};
let generator = Generator::new(GeneratorOptions::default())?;Sourcepub fn last_error_code(&self) -> i32
pub fn last_error_code(&self) -> i32
Returns the last error number from the library.
This can be useful for debugging after a failed operation.
Sourcepub fn error_description(&self, code: i32) -> Option<&str>
pub fn error_description(&self, code: i32) -> Option<&str>
Returns a human-readable description for an error code.
Sourcepub fn library_version(&self) -> i32
pub fn library_version(&self) -> i32
Returns the library version as a packed integer.
High 16 bits = major version, low 16 bits = minor version.
Sourcepub fn library_version_major(&self) -> i32
pub fn library_version_major(&self) -> i32
Returns the major version number.
Sourcepub fn library_version_minor(&self) -> i32
pub fn library_version_minor(&self) -> i32
Returns the minor version number.
Sourcepub fn library_version_patch(&self) -> i32
pub fn library_version_patch(&self) -> i32
Returns the patch version number.
Sourcepub fn library_version_text(&self) -> Option<&str>
pub fn library_version_text(&self) -> Option<&str>
Sourcepub fn compute_hash_rgb(
&self,
image_data: &[u8],
width: u32,
height: u32,
) -> Result<Hash>
pub fn compute_hash_rgb( &self, image_data: &[u8], width: u32, height: u32, ) -> Result<Hash>
Computes a PhotoDNA hash from RGB pixel data.
This is a convenience method that calls compute_hash
with RGB pixel format.
§Arguments
image_data- Raw RGB pixel data (3 bytes per pixel).width- Image width in pixels (minimum 50).height- Image height in pixels (minimum 50).
§Errors
Returns an error if:
- Image dimensions are too small (< 50 pixels)
- Buffer is too small for the specified dimensions
- Image has insufficient gradient (flat image)
§Examples
let hash = generator.compute_hash_rgb(&rgb_pixels, 640, 480)?;Sourcepub fn compute_hash(
&self,
image_data: &[u8],
width: u32,
height: u32,
options: HashOptions,
) -> Result<Hash>
pub fn compute_hash( &self, image_data: &[u8], width: u32, height: u32, options: HashOptions, ) -> Result<Hash>
Computes a PhotoDNA hash from pixel data with custom options.
§Arguments
image_data- Raw pixel data in the format specified byoptions.width- Image width in pixels (minimum 50).height- Image height in pixels (minimum 50).options- Hash computation options.
§Errors
Returns an error if the hash cannot be computed. See PhotoDnaError
for possible error types.
§Examples
use photodna::{Generator, GeneratorOptions, HashOptions, PixelFormat};
let generator = Generator::new(GeneratorOptions::default())?;
let options = HashOptions::new()
.pixel_format(PixelFormat::Bgra)
.remove_border(true);
let hash = generator.compute_hash(&bgra_pixels, 640, 480, options)?;Sourcepub fn compute_hash_with_stride(
&self,
image_data: &[u8],
width: u32,
height: u32,
stride: u32,
options: HashOptions,
) -> Result<Hash>
pub fn compute_hash_with_stride( &self, image_data: &[u8], width: u32, height: u32, stride: u32, options: HashOptions, ) -> Result<Hash>
Computes a PhotoDNA hash with explicit stride.
Use this when the image has padding bytes between rows (common in windowed framebuffers and some image formats).
§Arguments
image_data- Raw pixel data.width- Image width in pixels (minimum 50).height- Image height in pixels (minimum 50).stride- Row stride in bytes, or 0 to auto-calculate.options- Hash computation options.
§Errors
Returns an error if the hash cannot be computed.
Sourcepub fn compute_hash_subregion(
&self,
image_data: &[u8],
width: u32,
height: u32,
stride: u32,
region: (u32, u32, u32, u32),
options: HashOptions,
) -> Result<Hash>
pub fn compute_hash_subregion( &self, image_data: &[u8], width: u32, height: u32, stride: u32, region: (u32, u32, u32, u32), options: HashOptions, ) -> Result<Hash>
Computes a hash for a sub-region of an image.
§Arguments
image_data- Raw pixel data for the full image.width- Full image width in pixels.height- Full image height in pixels.stride- Row stride in bytes, or 0 to auto-calculate.region- The sub-region to hash: (x, y, width, height).options- Hash computation options.
§Errors
Returns an error if the region is outside the image bounds or if the hash cannot be computed.
Sourcepub fn compute_hash_with_border_detection(
&self,
image_data: &[u8],
width: u32,
height: u32,
options: HashOptions,
) -> Result<BorderHashResult>
pub fn compute_hash_with_border_detection( &self, image_data: &[u8], width: u32, height: u32, options: HashOptions, ) -> Result<BorderHashResult>
Computes a hash with automatic border detection.
This method returns both the original hash and a hash computed after removing detected borders.
§Arguments
image_data- Raw pixel data.width- Image width in pixels (minimum 50).height- Image height in pixels (minimum 50).options- Hash computation options.
§Returns
A BorderHashResult containing the primary hash and optionally
a hash with borders removed.
§Errors
Returns an error if the hash cannot be computed.
Sourcepub fn raw_instance(&self) -> *mut c_void
pub fn raw_instance(&self) -> *mut c_void
Returns the raw library instance pointer.
This is intended for advanced use cases that need direct FFI access.
§Safety
The returned pointer is only valid while this Generator is alive.
Do not use after dropping the generator.