commit 4f16af745526e95b661c14f351d630f54bf55f0f
parent bcdb459e06604ebe066cebaa4f971bd68e2e0a70
Author: Jack Mordaunt <jackmordaunt@gmail.com>
Date: Mon, 31 Dec 2018 21:59:25 +1300
[+] Create resized images in parallel.
Diffstat:
2 files changed, 19 insertions(+), 8 deletions(-)
diff --git a/icns.go b/icns.go
@@ -4,6 +4,7 @@ import (
"errors"
"image"
"io"
+ "sync"
"github.com/nfnt/resize"
)
@@ -61,19 +62,26 @@ func NewIconSet(img image.Image, interp InterpolationFunction) (*IconSet, error)
if biggest == 0 {
return nil, ErrImageTooSmall{image: img, need: 16}
}
- icons := []*Icon{}
- for _, size := range sizesFrom(biggest) {
+ icons := make([]*Icon, len(osTypes))
+ work := sync.WaitGroup{}
+ for ii, size := range sizesFrom(biggest) {
t, ok := getTypeFromSize(size)
if !ok {
continue
}
- iconImg := resize.Resize(size, size, img, interp)
- icon := &Icon{
- Type: t,
- Image: iconImg,
- }
- icons = append(icons, icon)
+ ii := ii
+ size := size
+ work.Add(1)
+ go func() {
+ iconImg := resize.Resize(size, size, img, interp)
+ icons[ii] = &Icon{
+ Type: t,
+ Image: iconImg,
+ }
+ work.Done()
+ }()
}
+ work.Wait()
iconset := &IconSet{
Icons: icons,
}
diff --git a/writer.go b/writer.go
@@ -109,6 +109,9 @@ func (s *IconSet) encodeIcons() error {
}
buf := bytes.NewBuffer(nil)
for _, icon := range s.Icons {
+ if icon == nil {
+ continue
+ }
if _, err := icon.WriteTo(buf); err != nil {
return err
}