@@ -23,6 +23,7 @@ macro_rules! console_log {
2323pub struct ImageOptions {
2424 pub width : u32 ,
2525 pub height : u32 ,
26+ pub channels : u32 ,
2627}
2728
2829fn str2f ( s : & str ) -> f64 {
@@ -33,7 +34,7 @@ fn str2f(s: &str) -> f64 {
3334 x
3435}
3536
36- fn img2vec ( im : & [ u8 ] , limit_max_side : Option < u32 > ) -> ( Vec < [ u8 ; 3 ] > , ImageOptions ) {
37+ fn img2vec ( im : & [ u8 ] , limit_max_side : Option < u32 > ) -> ( Vec < u8 > , ImageOptions ) {
3738 let img = image:: load_from_memory ( im) . expect ( "Failed to load image" ) ;
3839 let mut rgb = img. to_rgb8 ( ) ;
3940
@@ -50,25 +51,32 @@ fn img2vec(im: &[u8], limit_max_side: Option<u32>) -> (Vec<[u8; 3]>, ImageOption
5051 }
5152 }
5253
53- let mut pixels = Vec :: with_capacity ( rgb. width ( ) as usize * rgb. height ( ) as usize ) ;
54+ let mut pixels = Vec :: with_capacity ( rgb. width ( ) as usize * rgb. height ( ) as usize * 3 ) ;
5455 for pixel in rgb. pixels ( ) {
55- pixels. push ( [ pixel[ 0 ] , pixel[ 1 ] , pixel[ 2 ] ] ) ;
56+ pixels. push ( pixel[ 0 ] ) ;
57+ pixels. push ( pixel[ 1 ] ) ;
58+ pixels. push ( pixel[ 2 ] ) ;
5659 } ;
5760
5861 ( pixels, ImageOptions {
5962 width : rgb. width ( ) ,
6063 height : rgb. height ( ) ,
64+ channels : 3 ,
6165 } )
6266}
6367
64- fn vec2pngblob ( pixels : & Vec < [ u8 ; 3 ] > , im_opt : ImageOptions , limit_max_side : Option < u32 > ) -> Box < [ u8 ] > {
65- let ImageOptions { mut width, mut height } = im_opt;
68+ fn vec2pngblob ( pixels : & Vec < u8 > , im_opt : ImageOptions , limit_max_side : Option < u32 > ) -> Box < [ u8 ] > {
69+ let ImageOptions { mut width, mut height , channels } = im_opt;
6670
6771 let mut img = image:: RgbImage :: new ( width, height) ;
68- for ( i, pixel) in pixels. iter ( ) . enumerate ( ) {
72+ for ( i, pixel) in pixels. chunks ( channels as usize ) . enumerate ( ) {
6973 let x = ( i % width as usize ) as u32 ;
7074 let y = ( i / width as usize ) as u32 ;
71- img. put_pixel ( x, y, image:: Rgb ( * pixel) ) ;
75+ img. put_pixel ( x, y, image:: Rgb ( [
76+ pixel[ 0 ] ,
77+ pixel[ 1 ] ,
78+ pixel[ 2 ] ,
79+ ] ) ) ;
7280 } ;
7381
7482 if let Some ( max_side) = limit_max_side {
0 commit comments