Image formats compared
This article compares common image file formats from a software developer's perspective.
There are several image file formats available. Which one is the best for my job? Which format can store my images best? Which format produces the smallest files? Let's compare the following general-purpose, common formats: BMP, GIF, PNG, JPEG, TIFF, PCX and TGA. They are all bitmap formats. Vector images are beyond the scope of this article.
The following table compares the features of these file formats.
A color mode dictates the maximum number of colors an image file can store. The more colors, the better quality, but the larger the file.
We define low-color as modes ranging from 2 up to 256 colors. This number of colors is generally suitable for drawings, logos and other simple graphics. Low-color image files typically contain a color palette, from which the colors are chosen.
We define high-color as modes beyond 256 colors. These modes are more suitable for photos and other images with a high color resolution. High-color image files typically contain no color palette. The palette is pre-defined by the image format. A pre-defined RGB color space is common. Each pixel is represented by 3 values: the intensity of red, green and blue. JPEG uses another approach, but the result is the same: no color palette and lots of colors.
Today's computers can usually display 16 million colors, which is also the usual number of colors in photo files. In the past, display modes such as 16 or 256 colors were common. 16 million colors is considered enough for general purpose imaging. It is not always enough, though. PNG supports up to 256 teracolors (281,474,976,710,656 colors).
All these image formats support high-color images, with the exception for GIF. It supports low-color modes only. This makes GIF unsuitable for full-quality color photographs. When maximum quality is not required, or when the image is small, GIF can be reasonably used for color photos too. In addition, GIF provides adequate support for grayscale photos. You can see a 256-color sample photo later on this page.
Gray modes are specific modes for grayscale and/or black & white images. Some image formats support them, whereas some do not. When available, a gray mode saves disk space compared to the corresponding full-color mode, because there is no need to save color, just the light intensity. Even when there is no specific gray mode, it is always possible to save grayscale and black & white images using a normal color mode with a gray or black & white palette.
Monochrome modes are for 2-color images. Some image formats support a specific mode for black & white images (B&W). Other formats support a 2-color mode instead. It can store any 2-color image, including B&W. A 2-color mode is more versatile than pure B&W, because it can store any color pair, not just black and white. PNG has special capatibilites in that it has separate modes for pure B&W and other 2-color images.
Bit depth is the number of bits required to store a pixel. One needs a large bit depth to represent a large number of colors. The more accurate color representation is desired, the larger the bit depth. The maximum number of colors (or shades of gray) is determined by the following formula:
colors = 2bit depth
The following table lists the bit depth and its associated number of colors. Modes represented on a darker background are common. The other modes are less common, supported by a few image formats only.
To spare memory, disk space and bandwidth, it is advisable to save in the lowest possible color mode. Some unoptimized applications may save in an unnecessarily high color mode. A program might save a GIF in the 256-color mode even when the image contains just a few colors. This makes a web site load slower, for example.
The following images show a photo stored at various bit depths. The file size grows along with the number of colors.
Image data can be compressed to save memory and bandwidth. The following compression modes can be found in the image formats in question:
So, which file format and compression method provides the smallest file sizes? It depends on the image type. We tested this by saving the same image in different file formats. In each case, we used the maximum available compression along with the lowest possible color mode. For JPEG we used maximum compression without visible artefacts at 1:1 zoom level.
*) The 3-color PCX file was saved in a 16-color format, even though a lower color mode is available, due to limitations in the software at hand.
JPEG excels at photos but is lousy for other uses. PNG is relatively strong in all cases. It's the best choice for the 256-color screenshot, a typical use for lossless compression. GIF is a good choice for very low-color images.
BMP, TIFF, PCX and TGA have mediocre performance. They provide no clear advantage in terms of file size. Their use should be limited to where the particular format is really required.
The following file optimizers were used for the test. GIF: Animagic. PNG: OptiPNG.
Transparency. Some image formats support an optional, single transparent color. This means one color is transparent, the others are not. Alpha transparency means that colors can have a varying degree of transparency (typically 0..255). Each color (or pixel) can be either opaque, totally transparent or a mix of the color and the background. One is not forced to use transparency. Image formats that support transparency also support a non-transparent mode.
Interlacing is a technique that allows a partially downloaded image to be displayed as a low-resolution image. Interlacing has been useful for online use, especially when download speeds have been slow.
An interlaced image is stored in a specific order. The start of the file stores a low-resolution copy of the image. Subsequential bytes increase the resolution. A high-resolution image is available after all the bytes have been read.
When reading an interlaced GIF, you first get every 8th line, then every 4th line, then every 2nd line, and during the last pass, the rest of the lines. Due to this vertical interlacing, it is possible to show a low-resolution image after just 1/8 of the lines have been received. PNG combines vertical and horizontal interlacing. Whereas GIF stores each line in full, a PNG also interlaces in the x-direction, meaning just a few pixels per line at first. Because of this ordering, it is possible to show a low-resolution image after just 1/64 of the pixels have been received. JPEG uses yet another technique called Progressive JPEG.
There is usually no file size penalty in storing GIFs interlaced. On the contrary, interlaced PNG files may be larger due to compression disadvantages. Progressive JPEGs are not supported by all applications. Interlacing is optional. One can set it off.
Multiple images. Most image formats store only one image, possibly with an optional thumbnail. A GIF file can store multiple images. Most applications display them as an animated sequence, even though the GIF specification also has an unanimated option. A TIFF file can store multiple images, unanimated. It usually displays as a multi-page document. It is to be noted that several viewer applications only display the first image in a GIF or TIFF, hiding the rest of the images.
The PNG and PCX formats always store a single image. Multi-image versions of these formats do exist. MNG (Multiple-image Network Graphics) is the multi-image version of PNG. DCX is the multi-image version of PCX. Both MNG and DCX are relatively uncommon. They don't enjoy such wide application support as PNG and PCX. We are not going any further into these formats.
Max pixels is the absolute maximum pixel width and height allowed by the file format. In practice, applications have problems displaying much smaller images. They run out of memory or just fail to cope with images exceeding their internal limits.
64K×64K means 65535×65535 pixels. Applications might support only 32767×32767 pixels, though. This is because the commonly used signed short integer datatype supports values up to 32767.
2G means 2,147,483,647 pixels and 4G means 4,294,967,295 pixels.
The TGA format does not specify the maximum exactly. Width and height are stored in a 2-byte integer field. Whether the field is a signed or an unsigned integer is not specified. It is safer to assume a maximum of 32K×32K rather than 64K×64K.
Metadata and extensibility
Textual metadata. At times one wishes to record metadata: some information about the image. The information can be such as author name, image title, camera model, application name and so on.
Image formats differ in their ability to store metadata. This is a short description of the alternatives.
Numeric metadata. The above applies to textual metadata. One may also need to store numeric metadata, such as exposure time, resolution, orientation, gamma correction, creation date and so on. JPEG and TIFF support the EXIF format, which is quite a comprehensive set of metadata fields for photos. PNG and TGA provide some support too, which may not be as comprehensive as that of EXIF.
Extensibility. When pre-defined fields are not enough, a developer may wish to extend the image file format. The following image formats support a built-in mechanism for extensions: GIF, PNG, TIFF and TGA. An application can store private data in these files without harming interoperability with other applications. Such private data could be additional metadata, for example. Another possibility is to store the source data that was used create the image. Examples of extended file formats
BMP. The BMP format supports uncompressed storage and optional RLE compression in the 16-color and 256-color modes. It is advisable to store 2-color (monochrome) images in the 16-color RLE compressed mode. This makes the files smaller.
GIF. Saving in the lowest possible color mode keeps file sizes down. The size difference between a 2-color and 4-color image, though, is just 6 bytes, which equals the space required to store the larger color palette.
JPEG. The file format is actually JFIF (JPEG File Interchange Format). JPEG is a compression method for JFIF and TIFF files.
PCX. The PCX format supports several low-color modes, including two 4-color modes and an 8-color mode. Unfortunately PCX viewers differ in their interpretation of these formats. A viewer may display the image in a wrong way or even fail to work at all. Therefore, it's safer to save in the 16-color mode instead of 4 or 8 colors.
So, which format is the best choice? It depends on the use. GIF offers the best interoperability and good compression for low-color images. PNG is suitable for a wide range of image types and it has a great compression ratio too. JPEG may be the best choice for photos. For multi-page documents, TIFF is the usual choice. Where required, BMP, PCX and TGA are valid formats as well.
File format versions
This article is based on the following image format versions:
Image formats compared