Apple Icon Image format explained

Apple Icon Image
Uniform Type:com.apple.icns
Owner:Apple Inc.
Genre:Icon file format

The Apple Icon Image format (.icns) is an icon format used in Apple Inc.'s macOS. It supports icons of 16 × 16, 32 × 32, 48 × 48, 128 × 128, 256 × 256, 512 × 512 points at 1x and 2x scale, with both 1- and 8-bit alpha channels and multiple image states (example: open and closed folders). The fixed-size icons can be scaled by the operating system and displayed at any intermediate size.

As of macOS 11, asset catalogs are the preferred file format for macOS custom icons instead.[1]

File structure

The file format consists of an 8 byte header, followed by any number of icons.

Header

OffsetSizePurpose
04Magic literal, must be "icns" (0x69, 0x63, 0x6e, 0x73)
44Length of file, in bytes, msb first

Icon data

OffsetSizePurpose
04Icon type, see OSType below.
44Length of data, in bytes (including type and length), msb first
8VariableIcon data

Icon types

OSTypeLength (bytes)Size (pixels)Supported OS VersionDescription
12832×321.01-bit mono icon
25632×326.01-bit mono icon with 1-bit mask
4816×126.01 bit mono icon with 1-bit mask
9616×127.04 bit icon
19216×127.08 bit icon
6416×166.01-bit mono icon with 1-bit mask
12816×167.04-bit icon
25616×167.08 bit icon
varies (768)16×168.524-bit RGB icon
25616×168.58-bit mask
51232×327.04-bit icon
102432×327.08-bit icon
varies (3072)32×328.524-bit RGB icon
102432×328.58-bit mask
57648×488.51-bit mono icon with 1-bit mask
115248×488.54-bit icon
230448×488.58-bit icon
varies (6912)48×488.524-bit RGB icon
230448×488.58-bit mask
varies (49152 + 4)128×12810.024-bit RGB icon
16384128×12810.08-bit mask
varies16x1610.7JPEG 2000 or PNG format or 24-bit RGB icon
varies32x3210.7JPEG 2000 or PNG format or 24-bit RGB icon
varies48x4810.7JPEG 2000 or PNG format
varies128x12810.7JPEG 2000 or PNG format
varies256x25610.5JPEG 2000 or PNG format
varies512x51210.5JPEG 2000 or PNG format
varies1024x102410.7JPEG 2000 or PNG format (512x512@2x "retina" in 10.8)
varies32x3210.8JPEG 2000 or PNG format (16x16@2x "retina")
varies64x6410.8JPEG 2000 or PNG format (32x32@2x "retina")
varies256x25610.8JPEG 2000 or PNG format (128x128@2x "retina")
varies512x51210.8JPEG 2000 or PNG format (256x256@2x "retina")
varies (1024)16x16ARGB or JPEG 2000 or PNG format
varies (4096)32x32ARGB or JPEG 2000 or PNG format (16x16@2x "retina")
varies (1296)18x18ARGB or JPEG 2000 or PNG format
varies36x36JPEG 2000 or PNG format (18x18@2x "retina")
varies24x24JPEG 2000 or PNG format
varies48x48JPEG 2000 or PNG format (24x24@2x "retina")

Image data format

Compatibility

Other types

OSTypeDescription
"Table of Contents" a list of all image types in the file, and their sizes (added in Mac OS X 10.7).A TOC is written out as an identifier (4 bytes) and size (4 bytes). Each subsequent record (8 bytes each) mapsto the icon formats found in the file. The data isn't included in this phase.
4-byte big endian float - equal to the bundle version number of Icon Composer.app that created the icon
Usage unknown (all tested files use either "icon" or "template").
Info binary plist. Usage unknown (only name field seems to be used).
Nested "template" icns file. Usage unknown.
Nested "selected" icns file. Usage unknown.
Nested "dark" icns file. Allows automatic icon switching in Dark mode. (added in macOS 10.14).Note: The contents of this record is a full .icns file with multiple formats. If the record bytes are written outto disk, the icns file header and file size are still required to see the full dark mode icon.

Non-PNG / JPEG2000 Element Types

Element types that deal with ARGB (32-bit) or RGB (24-bit) image formats require different types of headers before the binary data. It is important to note that this header is part of the image data and is not the 4-byte big endian icon element type value (e.g. ic04 or ic05).[2]

ARGB ElementsARGB images must have their binary portion of the image data preceded by the four byte 'ARGB' header. After that, instead of each pixel with each of its four channels stored together (e.g. ARGBARGBARGB), an image with three pixels would be stored in individual channels of pixel data (e.g. AAARRRGGGBBB). In addition, each channel of pixel data needs to be encoded as mentioned below.

RGB ElementsRGB images have their binary portion of the image data preceded by four zero byte characters only when the element type is 'it32'. In all other cases, no header is needed. Channel data is separated as with the ARGB binary data (e.g. RRRGGGBBB instead of RGBRGBRGB). Each channel must also be encoded as mentioned below.

Mask ElementsMask elements are not encoded like ARGB and RGB image color channel data. The data is the same as that of an ARGB image except only the alpha channel data is provided. So for an image that has two pixels, ARGBARGB, the mask data is AA.

Compression

lead
tail
result
bytes
byte copies

Over time the format has been improved and there is support for compression of some parts of the pixel data. The 24-bit RGB and ARGB pixel data are compressed (per channel) with a format similar to PackBits.[3] Some sources mention that the OS supports both compressed or uncompressed data chunks. However, manually crafting icns files with uncompressed 24-bit RGB or ARGB images will not display properly – at least on newer macOS releases (tested on macOS 11).

Here is a GitHub repo with some swift code that appears to pass the test for both encoding and decoding as described here: ByteRunLengthCoder

The following pseudocode decompresses the data:Example: should decompress to

The following pseudocode compresses the data:Example: should compress to

Known issues

As of macOS 11, there are certain issues / bugs with the file format:

  1. Setting or will display a proper icon. But setting ignores the transparency mask and displays an icon without transparency.
  2. Compressed ARGB data is not interpreted correctly. The last value of the blue channel (aka. the very last value) is ignored and treated as if it were all zero-bytes. Usually this is no issue since most icons will have transparency at the bottom right corner anyway. However, it can become an issue if the last value is a repeating byte (see Compression). Potentially, up to 130 pixels can lack the blue channel value.
    A workaround is to append an additional byte at the end which is interpreted as a control character without following data. You can compare the difference with these two examples:
          1. macOS 10.15.7 (likely earlier) and later versions have an issue displaying PNG and JPEG 2000 icons for the keys (16x16), (32x32), and (64x64). The keys work fine in a standalone icns file but if used in an application, the icons are displayed completely scrambled. Either use the new ARGB format and (macOS 11+) or the old 24-bit RGB + alpha mask format. Use the latter with the old keys and, or with the newer keys and (writing RGB data into PNG fields). If using ARGB image data, make sure to provide alternative formats for macOS 10.15 and earlier. This issue is especially tricky to detect if you provide both, 16x16 and 16x16@2x icons, because if you connect your Mac to a non-retina monitor, the non-retina 16x16 icon will be used and thus the icon will be displayed scrambled. The field does not seem to be used in application icons and can safely be ignored. Additionally, if you don't provide the smaller icon sizes at all the bug will also manifest when the OS scales down your larger PNG/JPEG 2000 icons, so make sure to render smaller sizes and include them.

Support

Various image viewers can load *.icns files, and free and open source converters from or to PNG also exist.[4] [5] GTK+ can load *.icns resources since 2007.[6] Other tools supporting the format include the Apple Icon Composer and icns Browser, The Iconfactory, and IconBuilder.
MacOS offers the built-in iconutil command line tool to pack and unpack *.icns files.

See also

References

  1. Web site: Human Interface Guidelines. live. April 10, 2021. Apple Inc.. https://web.archive.org/web/20180618155438/https://developer.apple.com/design/human-interface-guidelines/macos/icons-and-images/app-icon/ . June 18, 2018 .
  2. https://github.com/fiahfy/icns.git Fiahfy ICNS Repo
  3. http://www.macdisk.com/maciconen.php#RLE Macintosh Icons
  4. Web site: libicns. SourceForge project icns. 2009. 2016-08-18.
  5. Web site: png2icns. Moin Uddin. 2016. 2017-05-25.
  6. Web site: Mac OS X icons for GTK+. Lyonel Vincent. 2007. 2016-08-18.

External links