iscc-lib 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Cargo.lock +2027 -0
- data/Cargo.toml +17 -6
- data/README.md +6 -3
- data/core/CLAUDE.md +177 -0
- data/core/Cargo.lock +959 -0
- data/core/Cargo.toml +131 -0
- data/core/README.md +84 -0
- data/core/benches/benchmarks.rs +294 -0
- data/core/benches/iai_benches.rs +171 -0
- data/core/src/cdc.rs +488 -0
- data/core/src/codec.rs +2074 -0
- data/core/src/conformance.rs +754 -0
- data/core/src/dct.rs +148 -0
- data/core/src/lib.rs +2693 -0
- data/core/src/minhash.rs +286 -0
- data/core/src/simhash.rs +356 -0
- data/core/src/streaming.rs +688 -0
- data/core/src/types.rs +116 -0
- data/core/src/utils.rs +361 -0
- data/core/src/wtahash.rs +389 -0
- data/core/src/xxh32.rs +134 -0
- data/core/tests/codec_clean.rs +247 -0
- data/core/tests/data.json +645 -0
- data/core/tests/test_algorithm_primitives.rs +303 -0
- data/core/tests/test_seq.rs +565 -0
- data/core/tests/test_strict_base32.rs +158 -0
- data/core/tests/test_text_utils.rs +157 -0
- data/lib/iscc_lib/version.rb +1 -1
- data/src/lib.rs +108 -25
- metadata +43 -4
data/core/src/dct.rs
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
//! Discrete Cosine Transform (DCT) for image content hashing.
|
|
2
|
+
//!
|
|
3
|
+
//! Implements the fast recursive DCT from Nayuki, matching the
|
|
4
|
+
//! reference implementation in `iscc-core/dct.py`.
|
|
5
|
+
//!
|
|
6
|
+
//! See: <https://www.nayuki.io/page/fast-discrete-cosine-transform-algorithms>
|
|
7
|
+
|
|
8
|
+
use std::f64::consts::PI;
|
|
9
|
+
|
|
10
|
+
use crate::IsccResult;
|
|
11
|
+
|
|
12
|
+
/// Compute the fast Discrete Cosine Transform (Nayuki algorithm).
|
|
13
|
+
///
|
|
14
|
+
/// Uses a recursive divide-and-conquer approach. Input length must be
|
|
15
|
+
/// a power of 2 (1, 2, 4, 8, 16, …). Returns f64 values matching the
|
|
16
|
+
/// reference implementation's floating-point behavior exactly.
|
|
17
|
+
pub(crate) fn alg_dct(v: &[f64]) -> IsccResult<Vec<f64>> {
|
|
18
|
+
let n = v.len();
|
|
19
|
+
if !n.is_power_of_two() {
|
|
20
|
+
return Err(crate::IsccError::InvalidInput(
|
|
21
|
+
"DCT input length must be a power of 2".into(),
|
|
22
|
+
));
|
|
23
|
+
}
|
|
24
|
+
Ok(dct_recursive(v))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/// Recursive fast DCT implementation.
|
|
28
|
+
fn dct_recursive(v: &[f64]) -> Vec<f64> {
|
|
29
|
+
let n = v.len();
|
|
30
|
+
if n == 1 {
|
|
31
|
+
return v.to_vec();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let half = n / 2;
|
|
35
|
+
|
|
36
|
+
// Split into symmetric (alpha) and antisymmetric (beta) parts
|
|
37
|
+
let alpha: Vec<f64> = (0..half).map(|i| v[i] + v[n - 1 - i]).collect();
|
|
38
|
+
let beta: Vec<f64> = (0..half)
|
|
39
|
+
.map(|i| (v[i] - v[n - 1 - i]) / ((i as f64 + 0.5) * PI / n as f64).cos() / 2.0)
|
|
40
|
+
.collect();
|
|
41
|
+
|
|
42
|
+
let alpha = dct_recursive(&alpha);
|
|
43
|
+
let beta = dct_recursive(&beta);
|
|
44
|
+
|
|
45
|
+
// Interleave results
|
|
46
|
+
let mut result = Vec::with_capacity(n);
|
|
47
|
+
for i in 0..half - 1 {
|
|
48
|
+
result.push(alpha[i]);
|
|
49
|
+
result.push(beta[i] + beta[i + 1]);
|
|
50
|
+
}
|
|
51
|
+
result.push(alpha[half - 1]);
|
|
52
|
+
result.push(beta[half - 1]);
|
|
53
|
+
|
|
54
|
+
result
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
#[cfg(test)]
|
|
58
|
+
mod tests {
|
|
59
|
+
use super::*;
|
|
60
|
+
|
|
61
|
+
#[test]
|
|
62
|
+
fn test_alg_dct_empty_error() {
|
|
63
|
+
assert!(alg_dct(&[]).is_err());
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
#[test]
|
|
67
|
+
fn test_alg_dct_odd_length_error() {
|
|
68
|
+
assert!(alg_dct(&[1.0, 2.0, 3.0]).is_err());
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
#[test]
|
|
72
|
+
fn test_alg_dct_all_zeros() {
|
|
73
|
+
let input = vec![0.0f64; 64];
|
|
74
|
+
let result = alg_dct(&input).unwrap();
|
|
75
|
+
for val in &result {
|
|
76
|
+
assert!(val.abs() < 1e-10, "expected ~0, got {val}");
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
#[test]
|
|
81
|
+
fn test_alg_dct_all_ones() {
|
|
82
|
+
let input = vec![1.0f64; 64];
|
|
83
|
+
let result = alg_dct(&input).unwrap();
|
|
84
|
+
assert!((result[0] - 64.0).abs() < 1e-10);
|
|
85
|
+
for &val in &result[1..] {
|
|
86
|
+
assert!(val.abs() < 1e-10, "expected ~0, got {val}");
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
#[test]
|
|
91
|
+
fn test_alg_dct_uniform_exact_zeros() {
|
|
92
|
+
// The Nayuki algorithm produces exact 0.0 for uniform input
|
|
93
|
+
// because v[i] - v[n-1-i] = 0 exactly.
|
|
94
|
+
let input = vec![255.0f64; 32];
|
|
95
|
+
let result = alg_dct(&input).unwrap();
|
|
96
|
+
assert_eq!(result[0], 255.0 * 32.0);
|
|
97
|
+
for &val in &result[1..] {
|
|
98
|
+
assert_eq!(val, 0.0, "expected exact 0.0, got {val}");
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
#[test]
|
|
103
|
+
fn test_alg_dct_range() {
|
|
104
|
+
let input: Vec<f64> = (0..64).map(|i| i as f64).collect();
|
|
105
|
+
let result = alg_dct(&input).unwrap();
|
|
106
|
+
assert!((result[0] - 2016.0).abs() < 1e-10);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
#[test]
|
|
110
|
+
fn test_alg_dct_single() {
|
|
111
|
+
let result = alg_dct(&[42.0]).unwrap();
|
|
112
|
+
assert!((result[0] - 42.0).abs() < 1e-10);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
#[test]
|
|
116
|
+
fn test_alg_dct_non_power_of_two_even_error() {
|
|
117
|
+
// Length 6 is even but not a power of 2 — must be rejected
|
|
118
|
+
assert!(alg_dct(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).is_err());
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
#[test]
|
|
122
|
+
fn test_alg_dct_length_10_error() {
|
|
123
|
+
// Length 10 is even but not a power of 2
|
|
124
|
+
assert!(alg_dct(&[1.0; 10]).is_err());
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
#[test]
|
|
128
|
+
fn test_alg_dct_length_12_error() {
|
|
129
|
+
// Length 12 is even but not a power of 2
|
|
130
|
+
assert!(alg_dct(&[1.0; 12]).is_err());
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
#[test]
|
|
134
|
+
fn test_alg_dct_length_2_ok() {
|
|
135
|
+
// Length 2 is a valid power of 2
|
|
136
|
+
assert!(alg_dct(&[1.0, 2.0]).is_ok());
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
#[test]
|
|
140
|
+
fn test_alg_dct_known_values() {
|
|
141
|
+
// DCT of [1, 2, 3, 4] matches Python reference
|
|
142
|
+
let result = alg_dct(&[1.0, 2.0, 3.0, 4.0]).unwrap();
|
|
143
|
+
assert!((result[0] - 10.0).abs() < 1e-10);
|
|
144
|
+
assert!((result[1] - (-3.15432202989895)).abs() < 1e-10);
|
|
145
|
+
assert!(result[2].abs() < 1e-10);
|
|
146
|
+
assert!((result[3] - (-0.22417076458398263)).abs() < 1e-10);
|
|
147
|
+
}
|
|
148
|
+
}
|