@@ -132,14 +132,14 @@ impl Encoder for GifEncoder{
132132 pix[ 0 ] = pix[ 2 ] ;
133133 pix[ 2 ] = a;
134134 }
135- f = :: gif :: Frame :: from_rgb ( frame. w as u16 , frame. h as u16 , & mut pixels) ;
135+ f = from_rgb_with_stride ( frame. w as u16 , frame. h as u16 , & mut pixels, frame . stride as usize ) ;
136136 } else {
137137 for pix in pixels. chunks_mut ( 4 ) {
138138 let a = pix[ 0 ] ;
139139 pix[ 0 ] = pix[ 2 ] ;
140140 pix[ 2 ] = a;
141141 }
142- f = :: gif :: Frame :: from_rgba ( frame. w as u16 , frame. h as u16 , & mut pixels) ;
142+ f = from_rgba_with_stride ( frame. w as u16 , frame. h as u16 , & mut pixels, frame . stride as usize ) ;
143143 }
144144
145145 let mut encoder = :: gif:: Encoder :: new ( io, frame. w as u16 , frame. h as u16 , & [ ] ) . unwrap ( ) ;
@@ -159,3 +159,22 @@ impl Encoder for GifEncoder{
159159 }
160160}
161161
162+
163+ fn remove_padding ( width : u16 , pixels : & [ u8 ] , stride : usize ) -> Vec < u8 > {
164+ pixels. chunks ( stride) . flat_map ( |s| s[ 0 ..width as usize * 4 ] . iter ( ) . map ( |v| * v) ) . collect ( )
165+ }
166+ /// Creates a frame from pixels in RGBA format.
167+ ///
168+ /// *Note: This method is not optimized for speed.*
169+ pub fn from_rgba_with_stride ( width : u16 , height : u16 , pixels : & mut [ u8 ] , stride : usize ) -> :: gif:: Frame < ' static > {
170+ let mut without_padding = remove_padding ( width, pixels, stride) ;
171+ :: gif:: Frame :: from_rgba ( width, height, & mut without_padding)
172+ }
173+
174+ /// Creates a frame from pixels in RGB format.
175+ ///
176+ /// *Note: This method is not optimized for speed.*
177+ pub fn from_rgb_with_stride ( width : u16 , height : u16 , pixels : & [ u8 ] , stride : usize ) -> :: gif:: Frame < ' static > {
178+ let mut without_padding = remove_padding ( width, pixels, stride) ;
179+ :: gif:: Frame :: from_rgb ( width, height, & mut without_padding)
180+ }
0 commit comments