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.
- BMP is the Windows bitmap format. It can store many types of images, but file sizes can be large.
- GIF (Graphics Interchange Format) is an old low-color image format, common on the web. It is suitable for simple graphics such as drawings, logos and icons. A GIF can be animated.
- PNG (Portable Network Graphics) is another common web format. Especially suitable for web use are its compression ratio (except for photos) and transparency features.
- JPEG (Joint Photographic Experts Group) is the common format for photos. Its lossy compression algorithm is suited for squeezing photos to small files.
- TIFF (Tagged Image File Format) is a versatile image format. It has the ability to store multiple images, such as the pages of a scanned or faxed document.
- PCX (PiCture eXchange) is an old bitmap format.
- TGA (Truevision Graphics Adapter), also known as TARGA, is another old bitmap format.
The following table compares the features of these 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 |
Monochrome modes | 2 colors | 2 colors | 2 colors or B&W | — | B&W | B&W | B&W |
Compression | None, RLE (16/ |
LZW | Deflate | JPEG | None, CCITT (B&W), PackBits, LZW, JPEG |
RLE | None, RLE |
Transparency (optional) | None | Single color | Single color, Alpha |
None | None | None | None |
Interlacing (optional) | None | Line order | Pixel order | Progressive | None | None | None |
Multiple images | No | Yes, animated | No | No | Yes, pages | No | No |
Max pixels | 2G |
64K |
2G |
64K |
4G |
64K |
32K |
Textual metadata | None | Comments | Keyword = value pairs | Comment, EXIF, IPTC |
Pre-defined fields, EXIF, IPTC |
None | Pre-defined fields |
Numeric metadata | None | None | Yes | EXIF, IPTC |
EXIF, IPTC |
None | Yes |
Extensible | 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.
Bits | Colors | Description | Common uses |
---|---|---|---|
1 | 2 | Monochrome. B&W or any 2 colors. | Icons, cursors |
2 | 4 | ||
3 | 8 | ||
4 | 16 | Color palette or grayscale palette | Icons |
5 | 32 | ||
6 | 64 | ||
7 | 128 | ||
8 | 256 | Color palette or full grayscale | Drawings, icons, grayscale images |
15 | 32768 | ||
16 | 65536 | Sometimes called "High Color" | |
24 | 16M | 16 million colors, "True Color" | Photos |
48 | 256T | 256 teracolors | Scans, 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.
2 colors, 2 kB |
4 colors, 3 kB |
8 colors, 4 kB |
16 colors, 5 kB |
32 colors, 6 kB |
64 colors, 9 kB |
128 colors, 11 kB |
256 colors, 12 kB |
16M colors, 27 kB |
256T colors, 74 kB |
Compression
Image data can be compressed to save memory and bandwidth. The following compression modes can be found in the image formats in question:
- None. Some file formats support an uncompressed mode, meaning the raw bits are stored exactly or very close to the way they appear on the screen.
- RLE (Run-Length Encoding) compression detects a continuous run of a single color and packs it in a few bits or bytes. Different variations to the idea exist. In particular, the TIFF format supports two alternative RLE compression methods: PackBits and CCITT. The CCITT compression comes in three variants and is only available for black & white images. RLE works well for images with long contiguous runs of a single color in the horizontal direction. This means RLE usually works well for monochrome or low-color drawings. RLE is ill-suited for photos.
- LZW (Lempel–Ziv–Welch) is an adaptive algorithm that looks for repetitive horizontal runs in the image. Rather than only packing continuous runs of a single color, it can effectively pack other types of repetition too. In a way, LZW "learns" the image as it compresses it. Once it finds a repeated location, it outputs a reference to the repetition rather than saving the same pixels again and again. It is to be noted that while both GIF and TIFF support LZW, there are subtle differences in the compression algorithm. Notably, GIF provides effective compression for small images with a low bit depth.
- Deflate is a compression algorithm employed by PNG. Deflate uses LZ77 compression encoded with Huffman codes. To make Deflate run more efficiently, PNG supports a filter step before the actual compression. The filtering doesn't modify the image. Instead, it converts the numerical values of pixels into ones that compress better. The combination of these techniques makes PNG superior to LZW compressed formats. PNG produces smaller files for many images, especially ones with color slides, and also for photos.
- JPEG is a lossy compression algorithm designed for photos. JPEG is effective for both color and grayscale images. It's supported by the JPEG and TIFF file formats. Applying JPEG compression to a non-photograph may result in low quality and a big file. A common error is to store drawings in the JPEG format.
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.
Test image | BMP | GIF | PNG | JPEG | TIFF (compressed) | PCX | TGA |
---|---|---|---|---|---|---|---|
16M-color photo | 43 kB | — | 27 kB | 7 kB | 44 kB (PackBits) | 42 kB | 43 kB |
256-gray photo | 16 kB | 14 kB | 10 kB | 6 kB | 15 kB (LZW) | 15 kB | 16 kB |
256-color screenshot | 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 (Packbits) | 448 B* | 954 B |
Monochrome drawing | 2990 B | 887 B | 745 B | 6727 B | 1274 B (CCITT |
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.
- BMP and PCX cannot store any metadata.
- GIF can store free-form comments in 7-bit ASCII.
Example:Portrait of composer Pyotr Tchaikovsky
- PNG can store
keyword=value
pairs using any characters.
Example:Title=Portrait of composer Pyotr Tchaikovsky (Пётр Чайковский)
- JPEG can store several types of metadata. There is a free-form comment field. One can also store textual metadata in pre-defined text fields in EXIF and IPTC formats. These formats allow information such as title, author, copyright, location etc. The exact contents of EXIF and IPTC are beyond the scope of this article.
- TIFF supports some pre-defined text fields in 7-bit ASCII. One can also store EXIF and IPTC metadata.
- TGA supports some pre-defined text fields in ASCII.
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:
- Bitmap (BMP): Microsoft Windows bitmaps
- Graphics Interchange Format (GIF): GIF89a
- Portable Network Graphics (PNG): v1.2 of 1999
- JPEG File Interchange Format (JFIF): v1.02 of 1992
- TIFF: Revision 6.0 of 1992
- PCX: No formal document
- Truevision TGA File Format: v2.0 of 1989
Image formats compared
URN:NBN:fi-fe200811172099