icns

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

commit 332bd5f294994e1f47adc2f7264813d7b746004b
parent 033c0cb80ab3768cc7de7c1aa21284bd29101d8c
Author: Jack Mordaunt <jackmordaunt@gmail.com>
Date:   Sun, 18 Feb 2018 16:00:39 +0100

[-] ICNS does not support standard JPEG.

Diffstat:
Micns.go | 17++++-------------
Micns_test.go | 8++++----
Mwriter.go | 25++++++-------------------
3 files changed, 14 insertions(+), 36 deletions(-)

diff --git a/icns.go b/icns.go @@ -12,7 +12,6 @@ import ( type Encoder struct { Wr io.Writer Algorithm InterpolationFunction - Format string } // NewEncoder initialises an encoder. @@ -28,13 +27,6 @@ func (enc *Encoder) WithAlgorithm(a InterpolationFunction) *Encoder { return enc } -// WithFormat applies the image format identifier used during registration by -// image/png and image/jpeg packages. -func (enc *Encoder) WithFormat(format string) *Encoder { - enc.Format = format - return enc -} - // Encode icns with the given configuration. func (enc *Encoder) Encode(img image.Image) error { if enc.Wr == nil { @@ -43,7 +35,7 @@ func (enc *Encoder) Encode(img image.Image) error { if img == nil { return errors.New("cannot encode nil image") } - iconset, err := NewIconSet(img, enc.Algorithm, enc.Format) + iconset, err := NewIconSet(img, enc.Algorithm) if err != nil { return err } @@ -64,7 +56,7 @@ func Encode(wr io.Writer, img image.Image) error { // NewIconSet uses the source image to create an IconSet. // If width != height, the image will be resized using the largest side without // preserving the aspect ratio. -func NewIconSet(img image.Image, interp InterpolationFunction, format string) (*IconSet, error) { +func NewIconSet(img image.Image, interp InterpolationFunction) (*IconSet, error) { biggest := findNearestSize(img) if biggest == 0 { return nil, ErrImageTooSmall{image: img, need: 16} @@ -77,9 +69,8 @@ func NewIconSet(img image.Image, interp InterpolationFunction, format string) (* } iconImg := resize.Resize(size, size, img, interp) icon := &Icon{ - Type: t, - Image: iconImg, - format: format, + Type: t, + Image: iconImg, } icons = append(icons, icon) } diff --git a/icns_test.go b/icns_test.go @@ -306,7 +306,7 @@ func TestEncodeImage(t *testing.T) { "jpg - jpg", _decode(_jpg(rect(0, 0, 50, 50))), "jpeg", - "jpeg", + "png", }, { "default jpg - png", @@ -321,15 +321,15 @@ func TestEncodeImage(t *testing.T) { "png", }, { - "not actually a jpeg: forces a conversion", + "not actually a jpeg", _decode(_png(rect(0, 0, 50, 50))), "jpeg", - "jpeg", + "png", }, } for _, tt := range tests { t.Run(tt.desc, func(st *testing.T) { - data, err := encodeImage(tt.img, tt.format) + data, err := encodeImage(tt.img) if err != nil { st.Fatalf("encoding image: %v", err) } diff --git a/writer.go b/writer.go @@ -3,16 +3,14 @@ package icns import ( "bytes" "image" - "image/jpeg" "image/png" "io" ) // Icon encodes an icns icon. type Icon struct { - Type OsType - Image image.Image - format string + Type OsType + Image image.Image header [8]byte headerSet bool @@ -42,7 +40,7 @@ func (i *Icon) encodeImage() error { if len(i.data) > 0 { return nil } - data, err := encodeImage(i.Image, i.format) + data, err := encodeImage(i.Image) if err != nil { return err } @@ -50,21 +48,10 @@ func (i *Icon) encodeImage() error { return nil } -func encodeImage(img image.Image, format string) ([]byte, error) { +func encodeImage(img image.Image) ([]byte, error) { buf := bytes.NewBuffer(nil) - switch format { - case "jpeg": - if err := jpeg.Encode( - buf, - img, - &jpeg.Options{Quality: 100}, - ); err != nil { - return nil, err - } - default: - if err := png.Encode(buf, img); err != nil { - return nil, err - } + if err := png.Encode(buf, img); err != nil { + return nil, err } return buf.Bytes(), nil }