Documentation
¶
Overview ¶
Package icns implements an encoder for Apple's `.icns` file format. Reference: "https://en.wikipedia.org/wiki/Apple_Icon_Image_format".
icns files allow for high resolution icons to make your apps look sexy. The most common ways to generate icns files are 1. use `iconutil` which is a Mac native cli utility, or 2. use tools that wrap `ImageMagick` which adds a large dependency to your project for such a simple use case.
With this library you can use pure Go to create icns files from any source image, given that you can decode it into an `image.Image`, without any heavyweight dependencies or subprocessing required. You can also use this library to create icns files on windows and linux.
A small CLI app `icnsify` is provided to allow you to create icns files using this library from the command line. It supports piping, which is something `iconutil` does not do, making it substantially easier to wrap.
Encode resizes one image into every size. EncodeSlots takes a drawing per slot, as an iconset directory holds, and resizes only the slots left empty. NewDecoder reads the other way, identifying the icons in a file so that a caller can decode only the size it wants.
Note: icns files are written with an icon at every size macOS draws, the retina OSTypes for the larger ones and the colour and mask pair Apple still uses at 16 and 32 pixels.
Index ¶
- Constants
- Variables
- func Decode(r io.Reader) (image.Image, error)
- func DecodeAll(r io.Reader) (images []image.Image, err error)
- func Encode(wr io.Writer, img image.Image) error
- type Decoder
- type Encoder
- type Entry
- type ErrImageTooSmall
- type Icon
- type IconDescription
- type IconSet
- type ImageFormat
- type InterpolationFunction
- type OsType
- type Slot
Constants ¶
const ( // Nearest-neighbor interpolation NearestNeighbor = resample.NearestNeighbor // Bilinear interpolation Bilinear = resample.Bilinear // Bicubic interpolation (with cubic hermite spline) Bicubic = resample.Bicubic // Mitchell-Netravali interpolation MitchellNetravali = resample.MitchellNetravali // Lanczos interpolation (a=2) Lanczos2 = resample.Lanczos2 // Lanczos interpolation (a=3) Lanczos3 = resample.Lanczos3 )
InterpolationFunction constants, ordered from fastest to highest quality.
Variables ¶
var ( // ErrInvalidHeader means the data does not begin with an icns header. ErrInvalidHeader = errors.New("invalid header for icns file") // ErrMalformed means an element's declared length disagrees with the // data present; the file is truncated or corrupt. ErrMalformed = errors.New("malformed icns file") // ErrNoIcons means the file is well formed but contains no icons of a // type this package understands. ErrNoIcons = errors.New("no icons found") // ErrUnsupportedFormat means every icon present uses an image format // this package cannot decode (JPEG 2000). ErrUnsupportedFormat = errors.New("unsupported image format") )
Errors returned by the decoder. They are wrapped with detail, so compare with errors.Is.
Functions ¶
func Decode ¶
Decode returns the largest icon in the icns file that can be decoded, ignoring all other sizes. An icon in a format no registered decoder reads is passed over, so the result may be smaller than the largest present.
Types ¶
type Decoder ¶ added in v4.1.0
type Decoder struct {
// contains filtered or unexported fields
}
Decoder reads an icns file and decodes its icons on demand, so a caller after one size does not pay for the rest.
func NewDecoder ¶ added in v4.1.0
NewDecoder reads r and identifies the icons it holds without decoding any of their pixels.
type Encoder ¶
type Encoder struct {
Wr io.Writer
Algorithm InterpolationFunction
}
Encoder encodes ICNS files from a source image.
func (*Encoder) EncodeSlots ¶ added in v4.1.0
EncodeSlots icns from artwork supplied per slot, so hand tuned art is used where it is given rather than resized from a single source.
func (*Encoder) WithAlgorithm ¶
func (enc *Encoder) WithAlgorithm(a InterpolationFunction) *Encoder
WithAlgorithm applies the interpolation function used to resize the image.
type Entry ¶ added in v4.1.0
type Entry struct {
IconDescription
// contains filtered or unexported fields
}
Entry is one icon in an icns file, before its pixels are decoded.
func (Entry) Decode ¶ added in v4.1.0
Decode decodes the icon's pixels. The formats icns defines itself are decoded here; an element holding a whole image file is passed to image.Decode, so it is read by whatever the program has registered.
func (Entry) Payload ¶ added in v4.1.0
Payload returns the bytes the file stores for the icon. For PNG and JPEG 2000 icons it is a complete image file; for the colour and mask types it is the run-length encoded colour planes, without the mask that holds their alpha.
The bytes are not copied, and must not be modified.
type ErrImageTooSmall ¶
type ErrImageTooSmall struct {
// contains filtered or unexported fields
}
ErrImageTooSmall is returned when the image is too small to process.
func (ErrImageTooSmall) Error ¶
func (err ErrImageTooSmall) Error() string
type IconDescription ¶
type IconDescription struct {
OsType
ImageFormat
}
IconDescription describes an icon.
func Probe ¶
func Probe(r io.Reader) (desc []IconDescription, _ error)
Probe extracts descriptions of the icons in the icns, largest first.
func (IconDescription) String ¶
func (desc IconDescription) String() string
type IconSet ¶
type IconSet struct {
Icons []*Icon
}
IconSet encodes a set of icons into an ICNS file.
func NewIconSet ¶
func NewIconSet(img image.Image, interp InterpolationFunction) (*IconSet, 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 NewIconSetFrom ¶ added in v4.1.0
NewIconSetFrom uses artwork supplied per slot to create an IconSet, which is what an iconset directory holds. A slot given artwork of the wrong size has it resized; a slot given none is filled from the largest image supplied, which also sets the largest icon written.
type ImageFormat ¶
type ImageFormat int
ImageFormat specifies the type of image data associated with an icon.
const ( ImageFormatPNG ImageFormat = iota ImageFormatJPEG2000 // ImageFormatRGB is 24-bit colour in run-length encoded channel planes, // with alpha held in a separate mask element. ImageFormatRGB // ImageFormatARGB is run-length encoded channel planes that carry their // own alpha, behind an "ARGB" header. ImageFormatARGB // ImageFormatBitmap is one bit per pixel with a one bit mask. ImageFormatBitmap // ImageFormatIndexed is an index per pixel into a fixed colour table. ImageFormatIndexed )
func (ImageFormat) String ¶
func (f ImageFormat) String() string
type InterpolationFunction ¶
InterpolationFunction is the algorithm used to resize the image.
type Slot ¶ added in v4.1.0
type Slot struct {
// Points is the size the icon is drawn at.
Points uint
// Scale is the display scale, 1 for a plain display and 2 for retina.
Scale uint
}
Slot identifies an icon by the size it is drawn at and the display scale it is drawn for, the way an iconset names its files. Artwork for 16x16@2x and for 32x32 is 32 pixels either way, but the two fill different slots and need not be the same drawing.
func ParseSlot ¶ added in v4.1.0
ParseSlot reads an iconset file name, such as "icon_16x16@2x.png". The boolean reports whether the name is one.
func Slots ¶ added in v4.1.0
func Slots() []Slot
Slots returns the slots an encoded icns holds, largest artwork first.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
icnsify
command
|
|
|
wasm
command
Command wasm exposes the icon encoders and decoders to a web page as the global "icns", so a conversion runs in the browser rather than on a server.
|
Command wasm exposes the icon encoders and decoders to a web page as the global "icns", so a conversion runs in the browser rather than on a server. |
|
Package ico implements an encoder and decoder for the Windows icon format.
|
Package ico implements an encoder and decoder for the Windows icon format. |
|
internal
|
|
|
resample
Package resample scales images for the icon formats in this module.
|
Package resample scales images for the icon formats in this module. |
