@@ -15,7 +15,7 @@ use usvg::{
1515#[ cfg( any( feature = "png" , feature = "jpeg" ) ) ]
1616use {
1717 image:: io:: Reader as ImageReader ,
18- image:: { DynamicImage , GenericImageView , ImageFormat , Rgb , Rgba } ,
18+ image:: { DynamicImage , GenericImageView , ImageFormat , Rgb , Rgba , Luma } ,
1919 pdf_writer:: writers:: ImageXObject ,
2020} ;
2121
@@ -611,17 +611,27 @@ impl Render for usvg::Image {
611611
612612 let bits = color. bits_per_pixel( ) ;
613613 let channels = color. channel_count( ) as u16;
614- let image_bytes: Vec < u8 > = if bits / channels > 8 {
615- decoded
614+ let image_bytes: Vec < u8 > = match ( channels, bits / channels > 8 ) {
615+ ( 1 , false ) => {
616+ decoded. to_luma8( ) . pixels( ) . flat_map( |& Luma ( c) | c) . collect( )
617+ }
618+ ( 1 , true ) => decoded
619+ . to_luma16( )
620+ . pixels( )
621+ . flat_map( |& Luma ( x) | x)
622+ . flat_map( |x| x. to_be_bytes( ) )
623+ . collect( ) ,
624+ ( 3 | 4 , false ) => {
625+ decoded. to_rgb8( ) . pixels( ) . flat_map( |& Rgb ( c) | c) . collect( )
626+ }
627+ ( 3 | 4 , true ) => decoded
616628 . to_rgb16( )
617629 . pixels( )
618630 . flat_map( |& Rgb ( c) | c)
619631 . flat_map( |x| x. to_be_bytes( ) )
620- . collect( )
621- } else {
622- decoded. to_rgb8( ) . pixels( ) . flat_map( |& Rgb ( c) | c) . collect( )
632+ . collect( ) ,
633+ _ => panic ! ( "unknown number of channels={channels}" ) ,
623634 } ;
624-
625635 let compressed = compress_to_vec_zlib( & image_bytes, 8 ) ;
626636
627637 let mut image = writer. image_xobject( image_ref, & compressed) ;
0 commit comments