@@ -221,6 +221,7 @@ pub struct WebPDecoder<R> {
221
221
222
222
kind : ImageKind ,
223
223
is_lossy : bool ,
224
+ has_alpha : bool ,
224
225
225
226
chunks : HashMap < WebPRiffChunk , Range < u64 > > ,
226
227
}
@@ -239,6 +240,7 @@ impl<R: Read + Seek> WebPDecoder<R> {
239
240
animation : Default :: default ( ) ,
240
241
memory_limit : usize:: MAX ,
241
242
is_lossy : false ,
243
+ has_alpha : false ,
242
244
} ;
243
245
decoder. read_data ( ) ?;
244
246
Ok ( decoder)
@@ -301,6 +303,7 @@ impl<R: Read + Seek> WebPDecoder<R> {
301
303
self . chunks
302
304
. insert ( WebPRiffChunk :: VP8L , start..start + chunk_size as u64 ) ;
303
305
self . kind = ImageKind :: Lossless ;
306
+ self . has_alpha = ( header >> 28 ) & 1 != 0 ;
304
307
}
305
308
WebPRiffChunk :: VP8X => {
306
309
let mut info = extended:: read_extended_header ( & mut self . r ) ?;
@@ -417,6 +420,7 @@ impl<R: Read + Seek> WebPDecoder<R> {
417
420
}
418
421
}
419
422
423
+ self . has_alpha = info. alpha ;
420
424
self . kind = ImageKind :: Extended ( info) ;
421
425
}
422
426
_ => return Err ( DecodingError :: ChunkHeaderInvalid ( chunk. to_fourcc ( ) ) ) ,
@@ -443,11 +447,7 @@ impl<R: Read + Seek> WebPDecoder<R> {
443
447
/// Returns whether the image has an alpha channel. If so, the pixel format is Rgba8 and
444
448
/// otherwise Rgb8.
445
449
pub fn has_alpha ( & self ) -> bool {
446
- match & self . kind {
447
- ImageKind :: Lossy => false ,
448
- ImageKind :: Lossless => true ,
449
- ImageKind :: Extended ( extended) => extended. alpha ,
450
- }
450
+ self . has_alpha
451
451
}
452
452
453
453
/// Returns whether the image is lossy. For animated images, this is true if any frame is lossy.
@@ -534,7 +534,11 @@ impl<R: Read + Seek> WebPDecoder<R> {
534
534
return Err ( DecodingError :: InconsistentImageSizes ) ;
535
535
}
536
536
537
- frame. fill_rgba ( buf) ;
537
+ if self . has_alpha {
538
+ frame. fill_rgba ( buf) ;
539
+ } else {
540
+ frame. fill_rgb ( buf) ;
541
+ }
538
542
} else {
539
543
let range = self
540
544
. chunks
0 commit comments