icns

Easily create .icns files (Mac Icons) with this Go library or the included CLI.
Log | Files | Refs | LICENSE

commit 608fd07fc64d417ba36ba11b8f8ca2bc085d14bd
parent 53997348b8910014cc39e77d121af9f7e8e381f3
Author: Jack Mordaunt <jackmordaunt@gmail.com>
Date:   Sat, 17 Feb 2018 23:59:13 +0100

[*] Factored out read icons into a type for readability.

Diffstat:
Mreader.go | 26+++++++++++++++-----------
1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/reader.go b/reader.go @@ -22,8 +22,7 @@ func Decode(r io.Reader) (image.Image, error) { return nil, errors.New("invalid header for icns file") } fileSize := binary.BigEndian.Uint32(data[4:8]) - icons := []OsType{} - datalist := []io.Reader{} + icons := []iconReader{} read := uint32(8) for read < fileSize { next := data[read : read+4] @@ -41,25 +40,30 @@ func Decode(r io.Reader) (image.Image, error) { read += 4 iconData := data[read : read+iconSize] read += iconSize - icons = append(icons, osTypeFromID(string(next))) - datalist = append(datalist, bytes.NewBuffer(iconData)) + icons = append(icons, iconReader{ + OsType: osTypeFromID(string(next)), + r: bytes.NewBuffer(iconData), + }) } } - var max OsType - var maxData io.Reader - for ii, i := range icons { - if i.Size > max.Size { - max = i - maxData = datalist[ii] + var biggest iconReader + for _, icon := range icons { + if icon.Size > biggest.Size { + biggest = icon } } - img, _, err := image.Decode(maxData) + img, _, err := image.Decode(biggest.r) if err != nil { return nil, err } return img, nil } +type iconReader struct { + OsType + r io.Reader +} + func isOsType(ID string) bool { _, ok := getTypeFromID(ID) return ok