From 8838e41eb1769e99f7d48db319b94540fb82e2aa Mon Sep 17 00:00:00 2001 From: Mark Hoekveen Date: Sat, 11 May 2024 01:22:58 +0200 Subject: [PATCH] output fixes --- src/bin/dctfilename.rs | 4 +++- src/bin/experiment.rs | 3 ++- src/descriptors/dct.rs | 24 +++++++++++------------- src/store.rs | 2 +- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/bin/dctfilename.rs b/src/bin/dctfilename.rs index 72eedf3..4c42190 100644 --- a/src/bin/dctfilename.rs +++ b/src/bin/dctfilename.rs @@ -2,6 +2,7 @@ use image_similarity::descriptors::{DCT, Descriptor}; use std::env; use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _}; +use log::debug; struct Cfg { path: String @@ -31,7 +32,8 @@ fn main() { let img = image::open(cfg.path) .expect("Unable to open file"); let phash: u64 = desc.describe(img); - println!("{phash}\n{phash:b}"); + debug!("Phash integer:\n{phash}"); + debug!("Phash binary:\n{phash:b}"); let output = URL_SAFE_NO_PAD.encode(phash.to_be_bytes()); println!("{output}") } diff --git a/src/bin/experiment.rs b/src/bin/experiment.rs index e51f0e5..811b892 100644 --- a/src/bin/experiment.rs +++ b/src/bin/experiment.rs @@ -3,7 +3,8 @@ use std::collections::HashMap; use std::fs; fn main() { - //Init all descriptors: + env_logger::init(); + let desc = DCT::new(); //Initialize hashmap, empty or from cached file: diff --git a/src/descriptors/dct.rs b/src/descriptors/dct.rs index 597d003..2e04c8a 100644 --- a/src/descriptors/dct.rs +++ b/src/descriptors/dct.rs @@ -1,12 +1,13 @@ use image::{save_buffer, GenericImageView}; use std::f64::consts::{PI, SQRT_2}; -use crate::descriptors::{Descriptor, print_matrix, DCT}; +use crate::descriptors::{Descriptor, DCT, print_matrix}; +use log::debug; impl DCT { /// Returns a new DCT instance with a base quality DCT matrix. pub fn new() -> DCT { - let base_quantization_matrix: [u8; 64] = [ + let quantization_matrix: [u8; 64] = [ 16, 11, 10, 16, 24, 40, 51, 61, 12, 12, 14, 19, 26, 58, 60, 55, 14, 13, 16, 24, 40, 57, 69, 56, @@ -16,7 +17,7 @@ impl DCT { 49, 64, 78, 8, 10, 12, 12, 101, 72, 92, 95, 9, 11, 10, 103, 99, ]; - DCT { quantization_matrix: base_quantization_matrix } + DCT { quantization_matrix } } // Builds DCT with given quality value @@ -37,9 +38,7 @@ impl DCT { self } - pub fn dct(&self, img: image::DynamicImage) -> [f64; 64] { - //let qmatrix = self.quantization_matrix(self.quality); - //println!("Q-{} quantization matrix:\n{}", self.quality, print_matrix(qmatrix)); + fn dct(&self, img: image::DynamicImage) -> [f64; 64] { let mut dct_values: [f64; 64] = [0.0; 64]; img.save("resize.png").expect("Error saving file"); for u in 0..8 { @@ -65,13 +64,12 @@ impl DCT { (y*v*PI/16.0).cos() } dct_values[k] = alpha * sum; - //println!{"{}", dct_values[k]} } } dct_values } - pub fn idct(&self, dct_values: [f64; 64]) -> [u8; 64] { + fn idct(&self, dct_values: [f64; 64]) -> [u8; 64] { let mut reconstructed: [u8; 64] = [0; 64]; for k in 0..64 { let x = (k%8) as f64; @@ -107,24 +105,24 @@ impl Descriptor for DCT { fn describe(&self, img: image::DynamicImage) -> u64 { let img = self.resize(img); let mut dct_values = self.dct(img); - println!("DCT-coefficients:\n {}", print_matrix(dct_values)); + debug!("DCT-coefficients:\n {}", print_matrix(dct_values)); // Quantization: - println!("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(); } - println!("DCT-coefficients, quantized:\n {}", print_matrix(dct_values)); + debug!("DCT-coefficients, quantized:\n {}", print_matrix(dct_values)); // De-quantization: for i in 0..64 { dct_values[i] = dct_values[i] * self.quantization_matrix[i] as f64; } - println!("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); - println!("Reconstructed pixel values:\n{}", print_matrix(reconstructed)); + debug!("Reconstructed pixel values:\n{}", print_matrix(reconstructed)); save_buffer( "reconstructed.png", diff --git a/src/store.rs b/src/store.rs index 2f86e4e..20c6bcd 100644 --- a/src/store.rs +++ b/src/store.rs @@ -20,7 +20,7 @@ impl DescriptorStore { Ok(f) => rmp_serde::from_slice(&f).unwrap(), Err(_e) => HashMap::with_capacity(count), }; - DescriptorStore { map: map } + DescriptorStore { map } } /// Returns true iff the store already contains the key