@@ -25,11 +25,11 @@ pub struct ImageOptions {
2525 pub height : u32 ,
2626}
2727
28- fn str2f ( s : & str ) -> f32 {
28+ fn str2f ( s : & str ) -> f64 {
2929 let mut hasher = DefaultHasher :: new ( ) ;
3030 s. hash ( & mut hasher) ;
3131 let hash = hasher. finish ( ) ;
32- let x = ( hash as f32 ) / ( u64:: MAX as f32 ) ;
32+ let x = ( hash as f64 ) / ( u64:: MAX as f64 ) ;
3333 x
3434}
3535
@@ -40,9 +40,9 @@ fn img2vec(im: &[u8], limit_max_side: Option<u32>) -> (Vec<[u8; 3]>, ImageOption
4040 if let Some ( max_side) = limit_max_side {
4141 let ( mut width, mut height) = rgb. dimensions ( ) ;
4242 if width > max_side || height > max_side {
43- let scale = max_side as f32 / width. max ( height) as f32 ;
44- width = ( width as f32 * scale) . round ( ) as u32 ;
45- height = ( height as f32 * scale) . round ( ) as u32 ;
43+ let scale = max_side as f64 / width. max ( height) as f64 ;
44+ width = ( width as f64 * scale) . round ( ) as u32 ;
45+ height = ( height as f64 * scale) . round ( ) as u32 ;
4646 rgb = image:: imageops:: resize (
4747 & rgb, width, height,
4848 image:: imageops:: FilterType :: Lanczos3
@@ -73,9 +73,9 @@ fn vec2pngblob(pixels: &Vec<[u8; 3]>, im_opt: ImageOptions, limit_max_side: Opti
7373
7474 if let Some ( max_side) = limit_max_side {
7575 if width > max_side || height > max_side {
76- let scale = max_side as f32 / width. max ( height) as f32 ;
77- width = ( width as f32 * scale) . round ( ) as u32 ;
78- height = ( height as f32 * scale) . round ( ) as u32 ;
76+ let scale = max_side as f64 / width. max ( height) as f64 ;
77+ width = ( width as f64 * scale) . round ( ) as u32 ;
78+ height = ( height as f64 * scale) . round ( ) as u32 ;
7979 img = image:: imageops:: resize (
8080 & img, width, height,
8181 image:: imageops:: FilterType :: Lanczos3
@@ -102,12 +102,11 @@ pub fn encode(im: &[u8], secret: &str, max_side: i32) -> Box<[u8]> {
102102 console_log ! ( "Encoding image with secret: {}, max_side: {}" , secret, max_side) ;
103103 let max_side = if max_side < 1 { None } else { Some ( max_side as u32 ) } ;
104104
105- let ( mut im_v, im_opt) = img2vec ( im, max_side) ;
105+ let ( im_v, im_opt) = img2vec ( im, max_side) ;
106+ let seed = str2f ( & secret) ;
107+ console_log ! ( "Seed: {}" , seed) ;
106108
107- let pixels = logistic_map:: encode (
108- & mut im_v,
109- str2f ( & secret)
110- ) ;
109+ let pixels = logistic_map:: encode ( & im_v, seed) ;
111110
112111 vec2pngblob ( & pixels, im_opt, None )
113112}
@@ -118,11 +117,10 @@ pub fn decode(im: &[u8], secret: &str, max_side: i32) -> Box<[u8]> {
118117 let max_side = if max_side < 1 { None } else { Some ( max_side as u32 ) } ;
119118
120119 let ( im_v, im_opt) = img2vec ( im, None ) ;
120+ let seed = str2f ( & secret) ;
121+ console_log ! ( "Seed: {}" , seed) ;
121122
122- let pixels = logistic_map:: decode (
123- & im_v,
124- str2f ( & secret)
125- ) ;
123+ let pixels = logistic_map:: decode ( & im_v, seed) ;
126124
127125 vec2pngblob ( & pixels, im_opt, max_side)
128126}
0 commit comments