Graphics Library

The external graphics library is a complement to the types in the standard library. It provides the ability to load and save images from a number of common file formats.

The graphics library is located in the package graphics, and entirely implemented in a single shared library. Since this is an external library, it does not need to be distributed alongside applications where it is not needed.

The library provides a convenient API for common operations. This API consists of the following functions:

The library also provides a more advanced API for more control over the process. This API centers around the type graphics.ImageFormat, which represents an image format. You can then call the options member to create an graphics.FormatOptions instance that lets you either load a file and inspect the settings used to compress the file, or to set the settings and then save an image using those.

The library has the following functions for listing image formats:

On Linux, the graphics library uses libpng for decoding PNG images, and libjpegturbo for JPEG images. On Windows, the Windows Imaging API is used. BMP, PPM, and ICO use custom implementations that support the most common image types. For example, this means that compressed BMP formats will not work.

Loading Icons

At the moment, the ico file format is the only supported format that may contain more than one image. When an ico file is loaded using loadImage, the highest resolution version is picked by default. If you wish to access all versions of the image, use loadImageSet instead of loadImage and loadSet instead of load.

For convenience, the graphics library also provides a reader for ico files. As such, it is possible to place ico files in a package, and the Graphics library will load it for you automatically. The icon will be placed in the name tree in the form of a function that returns an graphics.ImageSet that represents the icon. The function will create a copy of the image, so you are free to modify it if you wish.