Introduction to File Formats
Over time there have been many different file formats for representing graphics. Most are footnotes to history, with only a few ever becoming wildly popular. In this book we talk about the image formats PNG (Portable Network Graphics), JPEG (Joint Pictures Expert Group, the committee that designed the format), and GIF (Graphic Interchange Format); the animation formats SVG (Scalable Vector Graphics), SWF (used by Macromedia Flash), and to a lesser extent GIF again; and the document formats PostScript and PDF (Portable Document Format).
All of these formats fall into one of two camps: raster or vector. A raster image is composed of dots, called pixels. Almost every image on a web page is a raster image, and the PNG, JPEG, and GIF formats are raster formats. The alternative to representing a picture as a series of dots is to represent it as you might draw it, with lines, curves, and filled colors. This is a vector image. The most noticeable difference between raster and vector images is how they scale. If you zoom in on a raster image, each pixel simply becomes a larger and larger spot, which leads to chunky images. A vector image, in contrast, gives infinite magnification.
Each raster file format represents an image’s pixels differently. These formats vary in how much color information they store, whether they can handle transparency in an image, and whether you have to pay to use them. For this reason, not all graphics formats are suited for all jobs. JPEG is generally much better for photos because it has good compression, for example, and GIF is the only raster image format that can handle animated sequences.
The limitations of a file format highlight its design goals. The flexible PNG format, for example, has a maximum size of approximately 2 Gigapixels × 2 Gigapixels. At a standard screen resolution of 72 pixels per inch, could store an image of approximately 470 miles × 470 miles. This would allow you to save a 1:1 life-sized image of most of New England in a single PNG file! By comparison, a GIF file has a maximum size of 75 feet × 75 feet, which would let you store a 1:1 image of a good-sized house. So, the next time you need a life-sized image of Rhode Island, you’ll know which file format to use.
Table 1-1 gives you some of the important details of the file formats covered in this book.
Format | Type | Max. color depth | Alpha channel | Max. image size (pixels) | Multiple images | Toolkits |
PNG | Raster | 8-bit indexed 16-bit grayscale 48-bit RGB +16 bits w/alpha | 16 bit | 2Gig x 2Gig | No | GD, GD::Graph, ImageMagick, Gimp; can be embedded in SWF, SVG |
JPEG | Raster | 12-bit grayscale 36-bit RGB 32-bit CMYK | No | 64K x 64K | No | ImageMagick, GD, Gimp; can be embedded in SWF, SVG |
GIF | Raster | 8-bit indexed | 1-bit transparency | 64K x 64K | Yes | ImageMagick, Gimp |
SVG | Vector | 24-bit | Limited by user agent | Limited by user agent | Yes | ImageMagick, Gimp |
SWF | Vector | 24-bit | 8-bit | 1Gig x 1Gig (twips) | Yes | Ming library |
PostScript | Document | Limited by display device | No | Limited by display device | Yes | PostScript module, ImageMagick, Gimp |
Document | Limited by user agent | Limited by user agent | Limited by user agent | Yes | PDF::API2, ImageMagick |
Let’s now nail down a few definitions that are used for all graphics formats, starting with color.
Get Perl Graphics Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.