icns

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

commit ce05490eaa42c1a34c59ebb2d36c77eb8b189460
parent 4d4e3548ef07398447e3b237a26960a8ab379938
Author: Jack Mordaunt <jackmordaunt@gmail.com>
Date:   Fri,  9 Feb 2018 13:51:16 +0100

[*] Misc fixes.

Image size guard to reject small images.
Fixed bug in `findNearestSize`.
Annotated panic in `getType` with the image size.

Diffstat:
Aerror.go | 22++++++++++++++++++++++
Micns.go | 7+++++--
2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/error.go b/error.go @@ -0,0 +1,22 @@ +package icns + +import ( + "fmt" + "image" +) + +// ErrImageTooSmall is returned when the image is too small to process. +type ErrImageTooSmall struct { + need int + image image.Image +} + +func (err ErrImageTooSmall) Error() string { + b := err.image.Bounds().Max + format := "image is too small: %dx%d, need at least %dx%d" + return fmt.Sprintf(format, b.X, b.Y, err.need, err.need) +} + +func panicf(format string, values ...interface{}) { + panic(fmt.Sprintf(format, values...)) +} diff --git a/icns.go b/icns.go @@ -27,6 +27,9 @@ func Encode(wr io.Writer, img image.Image) error { // preserving the aspect ratio. func NewIconSet(img image.Image, interp InterpolationFunction) (*IconSet, error) { biggest := findNearestSize(img) + if biggest == 0 { + return nil, ErrImageTooSmall{image: img, need: 16} + } icons := []*Icon{} for _, size := range sizesFrom(biggest) { iconImg := resize.Resize(size, size, img, interp) @@ -63,7 +66,7 @@ var sizes = []uint{ func findNearestSize(img image.Image) uint { size := biggestSide(img) for _, s := range sizes { - if size > s { + if size >= s { return s } } @@ -105,7 +108,7 @@ var types = map[uint]OsType{ func getType(size uint) OsType { v, ok := types[size] if !ok { - panic("could not select the correct icon type") + panicf("could not select the correct icon type for size %d", size) } return v }