What is an Image?
An image is a matrix of pixels. It is a 2-dimensional or rectangular array of pixels. The dimensions (width and height) of the rectangle is called as resolution of the image. There will be m x n pixels in an image, where m and n are width and height of the image respectively. For example, there will be 76800 pixels in an image of resolution 320 x 240.
You can download the full sourcecode below for reading, creating and displaying PGM images!! The sourcecode is available in Java, VB.NET and C# along with sample PGM images.
Types of Images
Each pixel is represented by one or more integers. These integers are called as intensity values which represent the brightness in that pixel. Based on color, images can be classified into three types namely binary (black & white), grayscale and color. For binary images, intensity values can be either 0 or 1. For grayscale and color images, intensity values range from 0 to 255. In grayscale images, there will be only one intensity value per pixel. In color images, there will be three intensity values per pixel (for red, blue and green). Some advanced color images that support transparency, contains four intensity values per pixel.
Based on compression, images can be classified into compressed and un-compressed images. In un-compressed images, these intensity values will be stored as integers (or bytes) and hence reading and writing such images are easy when compared to compressed images. In compressed images, the intensity values will be transformed into some form using special algorithms. The un-compressed and lossless-compressed images retain the original visual quality, whereas the quality of compressed images will be based on the compression-ratio used during compression. Examples of compressed image formats are JPEG, GIF, PNG, TIFF, etc. Examples of un-compressed image formats are PNM, BMP, etc.
PNM is the acronym for Portable Any Map. The PNM images are the simplest image file formats for processing. These files contain the actual pixel values without any compression or decomposition of the data into file sections; and hence these images are more suitable for any image processing application. PNM images are of three types, namely portable bit-maps (PBM), portable gray-maps (PGM), and portable pix-maps (PPM). These formats are a convenient (simple) method of saving image data as they are equally easy to read in ones own applications.
There is no explicit file format associated with PNM itself and is just an abstraction of the PBM, PGM, and PPM formats, i.e. the name PNM refers collectively to PBM, PGM, and PPM image file formats. These image files are distinguished by magic identifier that appears in the first line of the file. The magic identifiers are P1 for PBM images; P2 and P5 for PGM images; and P3 and P6 for PPM images.
Each PGM file has two segments internally: Image Header and Image Data. Image header contains three lines. The first line contains the magic identifier which uniquely identifies the type of image and the type of data contained in the Image Data. The second line starts with the character #and is a comment that provides some information about the image. Optionally, there can be any number of comment lines each beginning with #. In addition, a comment can be placed anywhere with a #character, the comment extends to the end of the line.
The third line contains three integers that are respectively the number of columns, number of rows and the maximum pixel value (usually 255). The last value of the third line gives the maximum value of the color components for the pixels and this allows the format to describe more than single byte (0..255) color values. The header lines are normally delineated by carriage returns and/or linefeeds.
Image Data contains pixel values either in binary or ASCII format as indicated by the magic identifier. Each image data line should not be longer than 70 characters. Each pixel value either binary of ASCII, has a value starting from 0 to the maximum pixel value, specified in the third header line. While not required by the format specification it is a standard convention to store the image in top to bottom, left to right order.
The following are all valid PNM headers:
P5 1024 788 255
1024 #the image width
788 #the image height
To access the data in PNM files, we read the header lines, set the number of columns and rows, and then read the rows into a data structure. Then we can process this data and write the new pixel values out to a new PNM file.
PBM File Format
PBM images are for storing black and white images. PBM stores single bit pixel image as a series of ASCII 0s or 1s. Traditionally 0 refers to white while 1 refers to black. The header is identical to PPM and PGM format except there is no maximum pixel value in the third header line, as it doesnot have any meaning here. The magic identifier for PBM is P1. The following is an example of a small bitmap in this format.
#P1 PBM example
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0
0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
PGM File Format
PGM images are grayscale image file formats. It is of two types, containing magic identifiers namely P2 and P5. P2 stores pixel values as the ASCII characters for the digits, delimited by spaces between consecutive pixel values. P5 writes the bytes without delimiters binary numbers from 0 to 255. The following image (lena.pgm) is a classical example of a PGM image:
Note: The image Lena has been in the public domain for almost half a century (it supposedly appeared in Playboy magazine originally). It is often used by image processing researchers around the world for comparison of their methods and algorithms with other ones and appears in most journals on image processing.
The following data shows a P2 PGM file for lena256.pgm. The datum 158, for example, is three ASCII characters for the respective digits 1, 5 and 8, followed by a space.
#P2 PGM Example
256 256 255
158 165 158 158 158 158 155 158 155 161 155 150 155 155 ………
P5 PGM image file is shown below; where each pixel is stored as a byte that is a binary number from 0 to 255 (text editors show 0 through 127 as ASCII characters and 128 through 255 at whatever code the editor uses for those values).
#P5 PGM Example
256 256 255
We see that the P5 type of PGM is packed (without delimiters) so there is a single byte for each pixel. This is also called the raw data format. The size of this file is 65,576 bytes. The P2 type of PGM uses a byte for each numerical symbol (digit) and therefore requires three bytes for each number greater than 99 and also uses a space character after each pixel value. Thus the file is nearly four times as large. This P2 file is 245,724 bytes. However, humans can read the P2 type of PGM file, whereas they can not read the ASCII characters of the packed bytes, which can appear different in different editors (characters 128 to 255).
PPM File Format
PPM images are RGB color images, which magic identifiers P3 for ASCII data and P6 for binary data. P6 image files are obviously smaller than P3 and much faster to read. Note that P6 PPM files can only be used for single byte colors. The components are stored in the usual order, red - green - blue.
#P3 PPM Example
0 0 0 0 0 0 0 0 0 15 0 15
0 0 0 0 15 7 0 0 0 0 0 0
0 0 0 0 0 0 0 15 7 0 0 0
15 0 15 0 0 0 0 0 0 0 0 0
PGM Utility in VB.NET
The following image is a part of the screenshot of Visual Studio Object Browser. It shows the properties, methods and functions of PGM class written in VB.NET. This class can be used to read an existing PGM image, create a new PGM image, save an existing PGM image in another path, display a PGM image in a windows form, convert any bitmap image into PGM image (ReadFromBitmap() method), and convert any PGM image into bitmap (BMP) file format (CreateBitmap()) method). The sourcecode can be downloaded below.
PGM Utility in C#
The following image shows the Object Browser screenshot of PGM class written in C#. This class is similar to the one written in VB.NET. This class can be used to read an existing PGM image, create a new PGM image, save an existing PGM image to another path, display a PGM image in a windows form, convert any bitmap image into PGM image (ReadFromBitmap() method), and convert any PGM image into bitmap (BMP) file format (CreateBitmap() method). The sourcecode can be downloaded below.
A windows-forms application is written to demonstrate the PGM class written in C#. The executable and source code of this demo application can be downloaded below. The following image shows a screenshot of the demo application.
A set of sample PGM images are available for download below. The samples included are:
- baboon.pgm (512x512)
- barbara.pgm (512x512)
- cameraman.pgm (256x256)
- f16.pgm (384x384, 512x512)
- f18.pgm (320x240)
- fishingboat.pgm (512x512)
- goldhill.pgm (512x512)
- houses.pgm (512x512)
- lena.pgm (384x384, 512x512)
- lighthouse.pgm (384x384, 512x512)
- man.pgm (512x512)
- peppers.pgm (384x384, 512x512)