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.

Properties of common image file formats
File format BMP GIF PNG JPEG TIFF PCX TGA
Max colors 16M 256 256T 16M 16M 16M 16M
Max shades of gray 256 256 65536 256 256 256 256
Color modes 2, 16, 256, 32768, 65536, 16M 2, 4, 8, 16, 32, 64, 128, 256 2, 4, 16, 256, 16M, 256T 16M 16, 256, 16M (4), (8), 16, 256, 16M 256, 65536, 16M
Low-color modes 2, 16, 256 2, 4, 8, 16, 32, 64, 128, 256 2, 4, 16, 256 16, 256 (4), (8), 16, 256 256
High-color modes 32768, 65536, 16M 16M, 256T 16M 16M 16M 65536, 16M
Gray modes 2, 4, 16, 256, 65536 256 2, 256, 65536 2 2, 256
Mono­chrome modes 2 colors 2 colors 2 colors or B&W B&W B&W B&W
Com­pres­sion None,
RLE
(16/256 colors)
LZW Deflate JPEG None,
CCITT (B&W),
Pack­Bits,
LZW,
JPEG
RLE None,
RLE
Trans­pa­ren­cy (op­tion­al) None Single color Single color,
Alpha
None None None None
Inter­lacing (op­tion­al) None Line order Pixel order Pro­gres­sive None None None
Multiple images No Yes, ani­mated No No Yes, pages No No
Max pixels 2G×2G 64K×64K 2G×2G 64K×64K 4G×4G 64K×64K 32K×32K
Textual meta­data None Com­ments Key­word = value pairs Com­ment,
EXIF,
IPTC
Pre-de­fined fields,
EXIF,
IPTC
None Pre-de­fined fields
Nume­ric meta­data None None Yes EXIF,
IPTC
EXIF,
IPTC
None Yes
Exten­sible No Yes Yes No Yes No Yes

Color modes

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.

BitsColorsDescriptionCommon uses
12Monochrome. B&W or any 2 colors.Icons, cursors
24
38
416Color palette or grayscale paletteIcons
532
664
7128
8256Color palette or full grayscaleDrawings, icons, grayscale images
1532768
1665536Sometimes called "High Color"
2416M16 million colors, "True Color"Photos
48256T256 teracolorsScans, scientific images

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.

Flower, hardly recognizable
2 colors, 2 kB
Flower, recognizable
4 colors, 3 kB
Flower, low quality
8 colors, 4 kB
Flower, low quality
16 colors, 5 kB
Flower, good
32 colors, 6 kB
Flower, good
64 colors, 9 kB
Flower, good
128 colors, 11 kB
Flower, good
256 colors, 12 kB
Flower, fine
16M colors, 27 kB
Flower, fine
256T colors, 74 kB
Photo ©2005 Tuomas Salste

Compression

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.

Image file sizes compared, maximum compression, minimum color mode
Test image BMP GIF PNG JPEG TIFF (com­pres­sed) PCX TGA
16M-color photo 43 kB 27 kB 7 kB 44 kB (Pack­Bits) 42 kB 43 kB
256-gray photo 16 kB 14 kB 10 kB 6 kB 15 kB (LZW) 15 kB 16 kB
256-color screen­shot 62 kB 25 kB 14 kB 109 kB 30 kB (LZW) 54 kB 57 kB
3-color flag 1286 B 125 B 132 B 1243 B 1926 B (Pack­bits) 448 B* 954 B
Mono­chrome drawing 2990 B 887 B 745 B 6727 B 1274 B (CCITT/Huff­man) 2380 B 2604 B

*) 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.

Features

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

Format specifics

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.

Conclusion

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
URN:NBN:fi-fe200811172099