icns

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

commit bc979c7cfe88f1a680eaf1ebeca33001ffcfcbaf
parent b2ca62f253e992b378f1dda924377798ea0428ba
Author: Jack Mordaunt <jackmordaunt.dev@gmail.com>
Date:   Fri, 18 Sep 2026 16:09:14 -0400

icns: skip resampling when the source is already icon-sized

The encoder resampled even when the source matched the icon size exactly,
which is the common case for the largest icon and the most expensive one to
compute. Passing it through keeps that icon identical to the source and drops
the encoder's slowest operation.

Diffstat:
Micns.go | 15+++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/icns.go b/icns.go @@ -76,10 +76,9 @@ func NewIconSet(img image.Image, interp InterpolationFunction) (*IconSet, error) for _, osType := range osTypes { work.Add(1) go func(iconIdx int, osType OsType, size uint) { - iconImg := resize.Resize(size, size, img, interp) icons[iconIdx] = &Icon{ Type: osType, - Image: iconImg, + Image: resizeSquare(img, size, interp), } work.Done() }(iconIdx, osType, size) @@ -93,6 +92,18 @@ func NewIconSet(img image.Image, interp InterpolationFunction) (*IconSet, error) return iconSet, nil } +// resizeSquare scales img into a size by size square, ignoring the source +// aspect ratio. An image already at that size is passed through untouched, +// which keeps the largest icon identical to the source and skips the most +// expensive resample in the common case. +func resizeSquare(img image.Image, size uint, interp InterpolationFunction) image.Image { + bounds := img.Bounds() + if bounds.Dx() == int(size) && bounds.Dy() == int(size) { + return img + } + return resize.Resize(size, size, img, interp) +} + // Big-endian. // https://golang.org/src/image/png/writer.go func writeUint32(b []uint8, u uint32) {