@@ -31,6 +31,7 @@ fn str2f(s: &str) -> f64 {
3131
3232fn maybe_resize ( img : image:: RgbImage , max_side : u32 ) -> ( image:: RgbImage , u32 , u32 ) {
3333 let ( width, height) = img. dimensions ( ) ;
34+
3435 if width > max_side || height > max_side {
3536 let scale = max_side as f64 / width. max ( height) as f64 ;
3637 let new_width = ( width as f64 * scale) . round ( ) as u32 ;
@@ -48,15 +49,18 @@ fn maybe_resize(img: image::RgbImage, max_side: u32) -> (image::RgbImage, u32, u
4849fn img2vec ( im : & [ u8 ] , limit_max_side : Option < u32 > ) -> ( Vec < u8 > , ImageOptions ) {
4950 let img = image:: load_from_memory ( im) . expect ( "Failed to load image" ) ;
5051 let mut rgb = img. to_rgb8 ( ) ;
52+
5153 if let Some ( max_side) = limit_max_side {
5254 ( rgb, _, _) = maybe_resize ( rgb, max_side) ;
5355 }
56+
5457 let mut pixels = Vec :: with_capacity ( rgb. width ( ) as usize * rgb. height ( ) as usize * 3 ) ;
5558 for pixel in rgb. pixels ( ) {
5659 pixels. push ( pixel[ 0 ] ) ;
5760 pixels. push ( pixel[ 1 ] ) ;
5861 pixels. push ( pixel[ 2 ] ) ;
5962 } ;
63+
6064 ( pixels, ImageOptions {
6165 width : rgb. width ( ) ,
6266 height : rgb. height ( ) ,
@@ -72,6 +76,7 @@ enum ImageType {
7276fn vec2imblob ( pixels : & Vec < u8 > , im_opt : ImageOptions , limit_max_side : Option < u32 > , im_type : ImageType ) -> Box < [ u8 ] > {
7377 let ImageOptions { mut width, mut height , channels} = im_opt;
7478 let mut img = image:: RgbImage :: new ( width, height) ;
79+
7580 for ( i, pixel) in pixels. chunks ( channels as usize ) . enumerate ( ) {
7681 let x = ( i % width as usize ) as u32 ;
7782 let y = ( i / width as usize ) as u32 ;
@@ -81,9 +86,11 @@ fn vec2imblob(pixels: &Vec<u8>, im_opt: ImageOptions, limit_max_side: Option<u32
8186 pixel[ 2 ] ,
8287 ] ) ) ;
8388 } ;
89+
8490 if let Some ( max_side) = limit_max_side {
8591 ( img, width, height) = maybe_resize ( img, max_side) ;
8692 }
93+
8794 let mut buf = Vec :: new ( ) ;
8895 match im_type {
8996 ImageType :: Png => {
@@ -98,6 +105,7 @@ fn vec2imblob(pixels: &Vec<u8>, im_opt: ImageOptions, limit_max_side: Option<u32
98105 } ,
99106 }
100107 console_log ! ( "Export image, dimensions: {}x{}" , width, height) ;
108+
101109 buf. into_boxed_slice ( )
102110}
103111
@@ -106,9 +114,10 @@ pub fn encode(im: &[u8], secret: &str, max_side: i32, as_type: &str) -> Box<[u8]
106114 console_log ! ( "Encoding image with secret: {}, max_side: {}" , secret, max_side) ;
107115 let max_side = if max_side < 1 { None } else { Some ( max_side as u32 ) } ;
108116
109- let ( im_v, im_opt) = img2vec ( im, max_side) ;
110117 let seed = str2f ( & secret) ;
111118 console_log ! ( "Seed: {}" , seed) ;
119+
120+ let ( im_v, im_opt) = img2vec ( im, max_side) ;
112121 let pixels = logistic_map:: encode :: < 3 > ( & im_v, seed) ;
113122 vec2imblob ( & pixels, im_opt, None , match as_type {
114123 "png" => ImageType :: Png ,
@@ -122,9 +131,10 @@ pub fn decode(im: &[u8], secret: &str, max_side: i32, as_type: &str) -> Box<[u8]
122131 console_log ! ( "Decoding image with secret: {}, max_side: {}" , secret, max_side) ;
123132 let max_side = if max_side < 1 { None } else { Some ( max_side as u32 ) } ;
124133
125- let ( im_v, im_opt) = img2vec ( im, None ) ;
126134 let seed = str2f ( & secret) ;
127135 console_log ! ( "Seed: {}" , seed) ;
136+
137+ let ( im_v, im_opt) = img2vec ( im, None ) ;
128138 let pixels = logistic_map:: decode :: < 3 > ( & im_v, seed) ;
129139 vec2imblob ( & pixels, im_opt, max_side, match as_type {
130140 "png" => ImageType :: Png ,
0 commit comments