DCT mask calculation updated
This commit is contained in:
@@ -33,7 +33,7 @@ fn main() {
|
|||||||
.expect("Unable to open file");
|
.expect("Unable to open file");
|
||||||
let phash: u64 = desc.describe(&img);
|
let phash: u64 = desc.describe(&img);
|
||||||
debug!("Phash integer:\n{phash}");
|
debug!("Phash integer:\n{phash}");
|
||||||
debug!("Phash binary:\n{phash:b}");
|
debug!("Phash binary:\n{phash:064b}");
|
||||||
let output = URL_SAFE_NO_PAD.encode(phash.to_be_bytes());
|
let output = URL_SAFE_NO_PAD.encode(phash.to_be_bytes());
|
||||||
println!("{output}")
|
println!("{output}")
|
||||||
}
|
}
|
||||||
|
|||||||
+58
-8
@@ -107,7 +107,7 @@ impl Descriptor for DCT {
|
|||||||
debug!("DCT-coefficients:\n {}", print_matrix(dct_values));
|
debug!("DCT-coefficients:\n {}", print_matrix(dct_values));
|
||||||
|
|
||||||
// Quantization:
|
// Quantization:
|
||||||
debug!("Using quantization matrix:\n{}", print_matrix(self.quantization_matrix));
|
//debug!("Using quantization matrix:\n{}", print_matrix(self.quantization_matrix));
|
||||||
for i in 0..64 {
|
for i in 0..64 {
|
||||||
dct_values[i] = (dct_values[i] / self.quantization_matrix[i] as f64).round();
|
dct_values[i] = (dct_values[i] / self.quantization_matrix[i] as f64).round();
|
||||||
}
|
}
|
||||||
@@ -119,11 +119,10 @@ impl Descriptor for DCT {
|
|||||||
for i in 0..64 {
|
for i in 0..64 {
|
||||||
dct_values[i] = dct_values[i] * self.quantization_matrix[i] as f64;
|
dct_values[i] = dct_values[i] * self.quantization_matrix[i] as f64;
|
||||||
}
|
}
|
||||||
debug!("DCT-coefficients, de-quantized:\n {}", print_matrix(dct_values));
|
//debug!("DCT-coefficients, de-quantized:\n {}", print_matrix(dct_values));
|
||||||
|
|
||||||
// Reconstruction original pixel values:
|
// Reconstruction original pixel values:
|
||||||
let reconstructed = self.idct(dct_values);
|
let reconstructed = self.idct(dct_values);
|
||||||
debug!("Reconstructed pixel values:\n{}", print_matrix(reconstructed));
|
|
||||||
|
|
||||||
save_buffer(
|
save_buffer(
|
||||||
"reconstructed.png",
|
"reconstructed.png",
|
||||||
@@ -135,13 +134,64 @@ impl Descriptor for DCT {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Calculating descriptor from dct values:
|
// Calculating descriptor from dct values:
|
||||||
let mut mask: u64 = 0;
|
|
||||||
for dct in dct_values {
|
// Zigzag order for our flattened array,
|
||||||
if dct > 0.0 {
|
// first 28 elements only
|
||||||
mask += 1
|
let zigzag: [usize; 28] = [
|
||||||
|
0,
|
||||||
|
1, 8,
|
||||||
|
16, 9, 2,
|
||||||
|
3, 10, 17, 24,
|
||||||
|
32, 25, 18, 11, 4,
|
||||||
|
5, 12, 19, 26, 33, 40,
|
||||||
|
48, 41, 34, 27, 20, 13, 6,
|
||||||
|
];
|
||||||
|
|
||||||
|
// Mask that indicates if a dct coefficient is positive or negative
|
||||||
|
// By convention, when sign bit is 1, number is negative
|
||||||
|
let mut sign_mask: u64 = 0;
|
||||||
|
// If first horizontal AC coefficient is negative
|
||||||
|
// This might account for horizontal flips when applied to all horizontal coefficients.
|
||||||
|
let sign_mult = dct_values[1].signum();
|
||||||
|
|
||||||
|
// Mask that indicates if a coefficient is bigger or smaller than previous in order
|
||||||
|
let mut pearson_mask: u64 = 0;
|
||||||
|
let mut prev = dct_values[0];
|
||||||
|
|
||||||
|
|
||||||
|
for i in zigzag {
|
||||||
|
let cur = dct_values[i];
|
||||||
|
if cur > prev {
|
||||||
|
pearson_mask += 1;
|
||||||
}
|
}
|
||||||
mask = mask << 1;
|
prev = cur;
|
||||||
|
|
||||||
|
let signum = dct_values[i].signum();
|
||||||
|
// Only multiply sign if dct-coefficient contains a horizontal component.
|
||||||
|
if
|
||||||
|
i % 8 != 0 && sign_mult * signum < 0.0
|
||||||
|
||
|
||||||
|
signum < 0.0
|
||||||
|
{
|
||||||
|
sign_mask += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shift masks
|
||||||
|
sign_mask = sign_mask << 1;
|
||||||
|
pearson_mask = pearson_mask << 1;
|
||||||
}
|
}
|
||||||
|
debug!("Sign mask: {:028b}", sign_mask);
|
||||||
|
debug!("Pearson mask: {:028b}", pearson_mask);
|
||||||
|
|
||||||
|
let mut mask = sign_mask;
|
||||||
|
debug!("Mask: {:064b}", mask);
|
||||||
|
mask = mask << 28;
|
||||||
|
debug!("Mask: {:064b}", mask);
|
||||||
|
mask += pearson_mask;
|
||||||
|
debug!("Mask: {:064b}", mask);
|
||||||
|
mask = mask << 8;
|
||||||
|
debug!("Mask: {:064b}", mask);
|
||||||
|
// TODO: Do something with these last 8 bits.
|
||||||
mask
|
mask
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user