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: Windows bitmap (BMP), Graphics Interchange Format (GIF), Portable Network Graphics (PNG), JPEG File Interchange Format, TIFF, PCX and Truevision TGA.

The following table compares the features of these file formats.

File format properties
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 mode. Some image formats support a specific mode for storing Black & White images (B&W). Other formats support a 2-color mode to store either a Black & White image or any 2-color image. A 2-color mode is more versatile than pure B&W as it can store a B&W or any other 2-color image. 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.

2 colors
2 colors, 2 kB
4 colors
4 colors, 3 kB
8 colors
8 colors, 4 kB
16 colors
16 colors, 5 kB
32 colors
32 colors, 6 kB
64 colors
64 colors, 9 kB
128 colors
128 colors, 11 kB
256 colors
256 colors, 12 kB
16M colors
16M colors, 27 kB
256T colors
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:

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

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. This is a technique where a low-resolution image can be obtained from a partially downloaded image. Interlacing is useful for online use, especially slow downloads. An interlaced image is stored in a specific order. The first part of the bytes are 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.

While the PNG and PCX formats always store a single image, multi-image versions do exist: the MNG and DCX formats. These formats are relatively uncommon. They don't enjoy such wide application support as PNG and PCX.

Max pixels. This is the absolute maximum pixel width and height that the file format allows. 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 (signed short integer). 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 metadata.
  • GIF can store free-form comments in 7-bit ASCII.
  • PNG can store keyword=value pairs using any characters. Example: Title=My photo
  • 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.
  • 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.

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

©Aivosto Oy -