Skip to content

Commit

Permalink
fix: sort images largest-first
Browse files Browse the repository at this point in the history
  • Loading branch information
JackMordaunt committed May 29, 2021
1 parent 535d9c9 commit 9f7f2ee
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"image"
"io"
"io/ioutil"
"sort"
)

var jpeg2000header = []byte{0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50, 0x20, 0x20}
Expand All @@ -19,13 +20,10 @@ func Decode(r io.Reader) (image.Image, error) {
if err != nil {
return nil, err
}
var biggest iconReader
for _, icon := range icons {
if icon.Size > biggest.Size {
biggest = icon
}
}
img, _, err := image.Decode(biggest.r)
sort.Slice(icons, func(ii, jj int) bool {
return icons[ii].OsType.Size > icons[jj].OsType.Size
})
img, _, err := image.Decode(icons[0].r)
if err != nil {
return nil, fmt.Errorf("decoding largest image: %w", err)
}
Expand All @@ -45,6 +43,13 @@ func DecodeAll(r io.Reader) (images []image.Image, err error) {
}
images = append(images, img)
}
sort.Slice(images, func(ii, jj int) bool {
var (
left = images[ii].Bounds().Size()
right = images[jj].Bounds().Size()
)
return (left.X * left.Y) > (right.X * right.Y)
})
return images, nil
}

Expand Down

0 comments on commit 9f7f2ee

Please sign in to comment.