@@ -192,15 +192,27 @@ fn read_entry<R: Read>(r: &mut R, spec: SpecCompliance) -> ImageResult<DirEntry>
192192 }
193193
194194 // buf[6..8]: may be bit depth (0 = unspecified) or vertical hotspot for CUR files
195- let bits_per_pixel = u16:: from_le_bytes ( buf[ 6 ..8 ] . try_into ( ) . unwrap ( ) ) ;
195+ let mut bits_per_pixel = u16:: from_le_bytes ( buf[ 6 ..8 ] . try_into ( ) . unwrap ( ) ) ;
196196 if spec == SpecCompliance :: Strict && bits_per_pixel > 256 {
197197 return Err ( DecoderError :: IcoEntryTooManyBitsPerPixelOrHotspot . into ( ) ) ;
198198 }
199199
200+ let color_count = buf[ 2 ] ;
201+
202+ // Some icons don't have a bit depth, only a color count. Convert the color count
203+ // to the minimum necessary bit depth.
204+ if bits_per_pixel == 0 {
205+ let count = match color_count {
206+ 0 => 256 ,
207+ c => u16:: from ( c) ,
208+ } ;
209+ bits_per_pixel = count. next_power_of_two ( ) . trailing_zeros ( ) as u16 ;
210+ }
211+
200212 Ok ( DirEntry {
201213 width : buf[ 0 ] ,
202214 height : buf[ 1 ] ,
203- color_count : buf [ 2 ] ,
215+ color_count,
204216 reserved : buf[ 3 ] ,
205217 num_color_planes,
206218 bits_per_pixel,
@@ -209,9 +221,9 @@ fn read_entry<R: Read>(r: &mut R, spec: SpecCompliance) -> ImageResult<DirEntry>
209221 } )
210222}
211223
212- /// Find the entry with the highest (color depth, size ).
224+ /// Find the entry with the highest (size, color depth).
213225///
214- /// If two entries have the same color depth and size , pick the first one.
226+ /// If two entries have the same size and color depth , pick the first one.
215227/// While ICO files with multiple identical size and bpp entries are rare, they
216228/// do exist. Since we can't make an educated guess which one is best, picking
217229/// the first one is a reasonable default.
@@ -221,8 +233,8 @@ fn best_entry(entries: Vec<DirEntry>) -> ImageResult<DirEntry> {
221233 . rev ( ) // ties should pick the first entry, not the last
222234 . max_by_key ( |entry| {
223235 (
224- entry. bits_per_pixel ,
225236 u32:: from ( entry. real_width ( ) ) * u32:: from ( entry. real_height ( ) ) ,
237+ entry. bits_per_pixel ,
226238 )
227239 } )
228240 . ok_or ( DecoderError :: NoEntries . into ( ) )
@@ -644,4 +656,27 @@ mod test {
644656 IcoDecoder :: with_spec_compliance ( std:: io:: Cursor :: new ( & data) , SpecCompliance :: Strict ) ;
645657 assert ! ( decoder_strict. is_err( ) ) ;
646658 }
659+
660+ #[ test]
661+ fn best_entry_selection ( ) {
662+ let data = vec ! [
663+ 0x00 , 0x00 , 0x01 , 0x00 , 0x02 , 0x00 , // Entry 1: 16x16, 32 bpp
664+ 16 , 16 , 0 , 0 , 1 , 0 , 32 , 0 , 8 , 0 , 0 , 0 , 38 , 0 , 0 , 0 ,
665+ // Entry 2: 32x32, 0 bpp, 0 color_count
666+ 32 , 32 , 0 , 0 , 1 , 0 , 0 , 0 , 8 , 0 , 0 , 0 , 46 , 0 , 0 , 0 ,
667+ ] ;
668+
669+ let mut r = std:: io:: Cursor :: new ( & data) ;
670+ let entries = read_entries ( & mut r, SpecCompliance :: Lenient ) . unwrap ( ) ;
671+
672+ assert_eq ! ( entries. len( ) , 2 ) ;
673+ assert_eq ! ( entries[ 0 ] . width, 16 ) ;
674+ assert_eq ! ( entries[ 0 ] . bits_per_pixel, 32 ) ;
675+
676+ assert_eq ! ( entries[ 1 ] . width, 32 ) ;
677+ assert_eq ! ( entries[ 1 ] . bits_per_pixel, 8 ) ;
678+
679+ let best = best_entry ( entries) . unwrap ( ) ;
680+ assert_eq ! ( best. width, 32 ) ;
681+ }
647682}
0 commit comments