@ -32,9 +32,11 @@ impl<'a> PerceptualHash for PHash<'a> {
* Returns a u64 representing the value of the hash
* Returns a u64 representing the value of the hash
* /
* /
fn get_hash ( & self , cache : & Option < Box < Cache > > ) -> u64 {
fn get_hash ( & self , cache : & Option < Box < Cache > > ) -> u64 {
match self . prepared_image . image {
Some ( ref image ) = > {
// Get the image data into a vector to perform the DFT on.
// Get the image data into a vector to perform the DFT on.
let width = self . prepared_image . image . width ( ) as usize ;
let height = self . prepared_image . image . height ( ) as usize ;
let width = image . width ( ) as usize ;
let height = image . height ( ) as usize ;
// Get 2d data to 2d FFT/DFT
// Get 2d data to 2d FFT/DFT
// Either from the cache or calculate it
// Either from the cache or calculate it
@ -45,7 +47,7 @@ impl<'a> PerceptualHash for PHash<'a> {
match c . get_matrix_from_cache ( & Path ::new ( self . prepared_image . orig_path ) , width as u32 ) {
match c . get_matrix_from_cache ( & Path ::new ( self . prepared_image . orig_path ) , width as u32 ) {
Some ( matrix ) = > matrix ,
Some ( matrix ) = > matrix ,
None = > {
None = > {
let matrix = create_data_matrix ( width , height , & self . prepared_ image) ;
let matrix = create_data_matrix ( width , height , & image ) ;
// Store this DFT in the cache
// Store this DFT in the cache
match c . put_matrix_in_cache ( & Path ::new ( self . prepared_image . orig_path ) , width as u32 , & matrix ) {
match c . put_matrix_in_cache ( & Path ::new ( self . prepared_image . orig_path ) , width as u32 , & matrix ) {
Ok ( _ ) = > { }
Ok ( _ ) = > { }
@ -55,7 +57,7 @@ impl<'a> PerceptualHash for PHash<'a> {
}
}
}
}
} ,
} ,
None = > create_data_matrix ( width , height , & self . prepared_ image)
None = > create_data_matrix ( width , height , & image )
} ;
} ;
// Only need the top left quadrant
// Only need the top left quadrant
@ -90,10 +92,13 @@ impl<'a> PerceptualHash for PHash<'a> {
}
}
// println!("Hash for {} is {}", prepared_image.orig_path, hash);
// println!("Hash for {} is {}", prepared_image.orig_path, hash);
hash
hash
} ,
None = > 0 u64
}
}
}
}
}
fn create_data_matrix ( width : usize , height : usize , prepared_image : & PreparedImage ) -> Vec < Vec < f64 > > {
fn create_data_matrix ( width : usize , height : usize , image : & super ::image ::ImageBuffer < super ::image ::Luma < u8 > , Vec < u8 > > ) -> Vec < Vec < f64 > > {
let mut data_matrix : Vec < Vec < f64 > > = Vec ::new ( ) ;
let mut data_matrix : Vec < Vec < f64 > > = Vec ::new ( ) ;
// Preparing the results
// Preparing the results
for x in 0 . . width {
for x in 0 . . width {
@ -102,8 +107,7 @@ fn create_data_matrix(width: usize, height: usize, prepared_image: &PreparedImag
let pos_x = x as u32 ;
let pos_x = x as u32 ;
let pos_y = y as u32 ;
let pos_y = y as u32 ;
data_matrix [ x ]
data_matrix [ x ]
. push ( prepared_image
. image
. push ( image
. get_pixel ( pos_x , pos_y )
. get_pixel ( pos_x , pos_y )
. channels ( ) [ 0 ] as f64 ) ;
. channels ( ) [ 0 ] as f64 ) ;
}
}