Skip to main content

chromaprint/fingerprint/
classifier.rs

1use super::filter::HaarFilter;
2use super::integral_image::RollingIntegralImage;
3use super::quantizer::Quantizer;
4
5/// Gray code lookup table for 2-bit encoding.
6const GRAY_CODE: [u32; 4] = [0, 1, 3, 2];
7
8/// A classifier pairs a Haar-like filter with a quantizer.
9/// Each classifier contributes 2 bits to the fingerprint.
10#[derive(Debug, Clone, Copy)]
11pub struct Classifier {
12    pub filter: HaarFilter,
13    pub quantizer: Quantizer,
14}
15
16impl Classifier {
17    /// Classify: apply filter, quantize result, return Gray-coded 2-bit value.
18    #[inline(always)]
19    pub fn classify(&self, image: &RollingIntegralImage, offset: usize) -> u32 {
20        let value = self.filter.apply(image, offset);
21        let quantized = self.quantizer.quantize(value);
22        GRAY_CODE[quantized as usize]
23    }
24}