Generator

Struct Generator 

Source
pub struct Generator { /* private fields */ }
Available on Windows or Linux or macOS only.
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 Generator per 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

Source

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())?;
Source

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.

Source

pub fn error_description(&self, code: i32) -> Option<&str>

Returns a human-readable description for an error code.

Source

pub fn library_version(&self) -> i32

Returns the library version as a packed integer.

High 16 bits = major version, low 16 bits = minor version.

Source

pub fn library_version_major(&self) -> i32

Returns the major version number.

Source

pub fn library_version_minor(&self) -> i32

Returns the minor version number.

Source

pub fn library_version_patch(&self) -> i32

Returns the patch version number.

Source

pub fn library_version_text(&self) -> Option<&str>

Returns the library version as a human-readable string.

§Examples
let generator = Generator::new(GeneratorOptions::default())?;
println!("Version: {}", generator.library_version_text().unwrap_or("unknown"));
Source

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)?;
Source

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 by options.
  • 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)?;
Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.