DCT mask calculation updated

This commit is contained in:
2024-05-12 14:37:41 +02:00
parent cb4c562214
commit 659b53ed5c
2 changed files with 59 additions and 9 deletions
+58 -8
View File
@@ -107,7 +107,7 @@ impl Descriptor for DCT {
debug!("DCT-coefficients:\n {}", print_matrix(dct_values));
// 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 {
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 {
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:
let reconstructed = self.idct(dct_values);
debug!("Reconstructed pixel values:\n{}", print_matrix(reconstructed));
save_buffer(
"reconstructed.png",
@@ -135,13 +134,64 @@ impl Descriptor for DCT {
}
// Calculating descriptor from dct values:
let mut mask: u64 = 0;
for dct in dct_values {
if dct > 0.0 {
mask += 1
// Zigzag order for our flattened array,
// first 28 elements only
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
}
}