#[non_exhaustive]pub enum Algorithm {
AverageV1,
DifferenceV1,
PerceptualV1,
SimHashV1,
PdqV1,
BlockhashV1,
}Expand description
A hash algorithm at one version of its definition.
The version is part of the algorithm’s identity. A released definition never changes its
output: a change ships as a new variant, such as PerceptualV2, beside the old one, which
stays. There’s no “latest” or “default” algorithm, so upgrading the crate never changes what an
application computes or stores.
use legit_hashes::Algorithm;
let algorithm: Algorithm = "perceptual-v1".parse()?;
assert_eq!(algorithm, Algorithm::PerceptualV1);
assert_eq!(algorithm.id(), "perceptual-v1");
assert_eq!(algorithm.bits(), 64);Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
AverageV1
average-v1: one bit per pixel of an 8×8 reduction, set when the pixel is brighter than
the mean. See average_v1.
DifferenceV1
difference-v1: one bit per neighbour pair of a 9×8 reduction, set when the right pixel is
brighter. See difference_v1.
PerceptualV1
perceptual-v1: the lowest 8×8 frequencies of a 32×32 reduction’s DCT, each set when it’s
above their median. See perceptual_v1.
SimHashV1
simhash-v1: Charikar’s SimHash of weighted text features, each hashed with SipHash-2-4.
See SimHashV1.
PdqV1
pdq-v1: Meta’s PDQ, 256 bits, the 16×16 DCT frequencies of a 64×64 reduction of the luma,
each set when it’s above their median. See pdq_v1.
BlockhashV1
blockhash-v1: the block mean value hash of Yang, Gu and Niu, 256 bits, one for each block
of a 16×16 grid, set when the block is brighter than the median of its band. See
blockhash_v1.
Implementations§
Source§impl Algorithm
impl Algorithm
Sourcepub const ALL: [Self; 6]
pub const ALL: [Self; 6]
Every algorithm this version of the crate knows, in the order they were added.
Sourcepub const fn id(self) -> &'static str
pub const fn id(self) -> &'static str
The stable text id, such as perceptual-v1, used in every stored and printed form.
Sourcepub const fn bits(self) -> u32
pub const fn bits(self) -> u32
The width of the algorithm’s hashes in bits: 64, or 256 for pdq-v1 and blockhash-v1.
Sourcepub fn from_id(id: &str) -> Result<Self>
pub fn from_id(id: &str) -> Result<Self>
Find the algorithm with the text id id, such as average-v1. The match is exact.
§Errors
Returns Error::NotABitHash for the id of a SignatureAlgorithm, such as
minhash-v1, and Error::UnknownAlgorithm when no algorithm has that id.
Sourcepub fn hash_luma(self, image: Luma<'_>) -> Result<Hash>
pub fn hash_luma(self, image: Luma<'_>) -> Result<Hash>
Hash an 8-bit luma image with this algorithm.
The same as calling average_v1,
difference_v1 or perceptual_v1, for
code that reads the algorithm from configuration. pdq-v1 hashes the levels as luma, as
pdq_v1_luma does, and drops the quality; blockhash-v1 hashes each
level v as the opaque colour (v, v, v), as RgbaBuf::from_luma
makes it.
use legit_hashes::{Algorithm, Luma};
let pixels = [0, 255, 0, 255];
let hash = Algorithm::DifferenceV1.hash_luma(Luma::new(2, 2, &pixels)?)?;
assert_eq!(hash.algorithm(), Algorithm::DifferenceV1);§Errors
Returns Error::NotAnImageAlgorithm for an algorithm that hashes text.
Sourcepub fn hash_rgba(self, image: Rgba<'_>) -> Result<Hash>
pub fn hash_rgba(self, image: Rgba<'_>) -> Result<Hash>
Hash an 8-bit RGBA image with this algorithm.
pdq-v1 and blockhash-v1 hash it as pdq_v1 and
blockhash_v1 do, dropping PDQ’s quality. The v1 image hashes
convert it to luma with LumaBuf::from_rgba8_v1, which ignores alpha, and hash that.
use legit_hashes::{Algorithm, RgbaBuf};
let image = RgbaBuf::from_rgb8(2, 1, &[255, 0, 0, 0, 0, 255])?;
let hash = Algorithm::BlockhashV1.hash_rgba(image.view())?;
assert_eq!(hash.bits(), 256);§Errors
Returns Error::NotAnImageAlgorithm for an algorithm that hashes text.
Sourcepub fn hash_encoded(self, bytes: &[u8]) -> Result<Hash>
pub fn hash_encoded(self, bytes: &[u8]) -> Result<Hash>
Decode a PNG or JPEG file and hash it with this algorithm.
The v1 image hashes read the file with decode_luma_v1 and hash
it with hash_luma. pdq-v1 and blockhash-v1 read it with
decode_rgba_v1 and hash it with hash_rgba,
so pdq-v1 hashes a greyscale file as RGB. Both decoders’ limits bound what a hostile file
can allocate.
§Errors
Returns Error::NotAnImageAlgorithm for an algorithm that hashes text, and
Error::Decode when the bytes aren’t a PNG or JPEG file that decodes within the limits.
Trait Implementations§
impl Copy for Algorithm
Source§impl<'de> Deserialize<'de> for Algorithm
Available on crate feature serde only.
impl<'de> Deserialize<'de> for Algorithm
serde only.