@@ -65,7 +65,12 @@ fn img2vec(im: &[u8], limit_max_side: Option<u32>) -> (Vec<u8>, ImageOptions) {
6565 } )
6666}
6767
68- fn vec2pngblob ( pixels : & Vec < u8 > , im_opt : ImageOptions , limit_max_side : Option < u32 > ) -> Box < [ u8 ] > {
68+ enum ImageType {
69+ Png ,
70+ Jpeg ,
71+ }
72+
73+ fn vec2imblob ( pixels : & Vec < u8 > , im_opt : ImageOptions , limit_max_side : Option < u32 > , im_type : ImageType ) -> Box < [ u8 ] > {
6974 let ImageOptions { mut width, mut height , channels} = im_opt;
7075
7176 let mut img = image:: RgbImage :: new ( width, height) ;
@@ -93,20 +98,25 @@ fn vec2pngblob(pixels: &Vec<u8>, im_opt: ImageOptions, limit_max_side: Option<u3
9398
9499 // Encode the image to PNG format
95100 let mut buf = Vec :: new ( ) ;
96- let encoder = image:: codecs:: png:: PngEncoder :: new ( & mut buf) ;
97- encoder. write_image (
98- & img,
99- width,
100- height,
101- image:: ExtendedColorType :: Rgb8 ,
102- ) . expect ( "Failed to encode image" ) ;
101+ match im_type {
102+ ImageType :: Png => {
103+ image:: codecs:: png:: PngEncoder :: new ( & mut buf)
104+ . write_image ( & img, width, height, image:: ExtendedColorType :: Rgb8 )
105+ . expect ( "Failed to encode PNG image" ) ;
106+ } ,
107+ ImageType :: Jpeg => {
108+ image:: codecs:: jpeg:: JpegEncoder :: new_with_quality ( & mut buf, 90 )
109+ . write_image ( & img, width, height, image:: ExtendedColorType :: Rgb8 )
110+ . expect ( "Failed to encode JPEG image" ) ;
111+ } ,
112+ }
103113
104114 console_log ! ( "Export image, dimensions: {}x{}" , width, height) ;
105115 buf. into_boxed_slice ( )
106116}
107117
108118#[ wasm_bindgen]
109- pub fn encode ( im : & [ u8 ] , secret : & str , max_side : i32 ) -> Box < [ u8 ] > {
119+ pub fn encode ( im : & [ u8 ] , secret : & str , max_side : i32 , as_type : & str ) -> Box < [ u8 ] > {
110120 console_log ! ( "Encoding image with secret: {}, max_side: {}" , secret, max_side) ;
111121 let max_side = if max_side < 1 { None } else { Some ( max_side as u32 ) } ;
112122
@@ -116,11 +126,15 @@ pub fn encode(im: &[u8], secret: &str, max_side: i32) -> Box<[u8]> {
116126
117127 let pixels = logistic_map:: encode ( & im_v, seed) ;
118128
119- vec2pngblob ( & pixels, im_opt, None )
129+ vec2imblob ( & pixels, im_opt, None , match as_type {
130+ "png" => ImageType :: Png ,
131+ "jpeg" => ImageType :: Jpeg ,
132+ _ => panic ! ( "Unsupported image type: {}" , as_type) ,
133+ } )
120134}
121135
122136#[ wasm_bindgen]
123- pub fn decode ( im : & [ u8 ] , secret : & str , max_side : i32 ) -> Box < [ u8 ] > {
137+ pub fn decode ( im : & [ u8 ] , secret : & str , max_side : i32 , as_type : & str ) -> Box < [ u8 ] > {
124138 console_log ! ( "Decoding image with secret: {}, max_side: {}" , secret, max_side) ;
125139 let max_side = if max_side < 1 { None } else { Some ( max_side as u32 ) } ;
126140
@@ -130,5 +144,9 @@ pub fn decode(im: &[u8], secret: &str, max_side: i32) -> Box<[u8]> {
130144
131145 let pixels = logistic_map:: decode ( & im_v, seed) ;
132146
133- vec2pngblob ( & pixels, im_opt, max_side)
147+ vec2imblob ( & pixels, im_opt, max_side, match as_type {
148+ "png" => ImageType :: Png ,
149+ "jpeg" => ImageType :: Jpeg ,
150+ _ => panic ! ( "Unsupported image type: {}" , as_type) ,
151+ } )
134152}
0 commit comments