Skip to content

Commit

Permalink
ico: update Decode to return the image with highest resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
lucor committed Apr 16, 2024
1 parent c3c798e commit d685569
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion ico/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ func Decode(r io.Reader) (image.Image, error) {
if err := d.decode(r); err != nil {
return nil, err
}
return d.images[0], nil
img := d.images[0]
// return the image with the highest resolution, if any
for i := 1; i < len(d.images); i++ {
if d.images[i].Bounds().Dx() > img.Bounds().Dx() {
img = d.images[i]
}
}
return img, nil
}

func DecodeAll(r io.Reader) ([]image.Image, error) {
Expand Down
10 changes: 10 additions & 0 deletions ico/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,13 @@ func TestDecodeAll(t *testing.T) {
}
}
}

func TestDecodeHighestRes(t *testing.T) {
expectedWidth := 32
rd, err := os.Open("testdata/multi.ico")
assert.NoError(t, err)
defer rd.Close()
image, err := ico.Decode(rd)
assert.NoError(t, err)
assert.Equal(t, expectedWidth, image.Bounds().Dx())
}
Binary file added ico/testdata/multi.ico
Binary file not shown.
Binary file added ico/testdata/multi.ico-0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ico/testdata/multi.ico-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d685569

Please sign in to comment.