SETTINGS
Appearance
Language
About

Settings

Select a category to the left.

Appearance

Theme

Light or dark? Choose how the site looks to you by clicking an image below.

Light Dark

Language

Preferred Language

All content on utk.claranguyen.me is originally in UK English. However, if content exists in your preferred language, it will display as that instead. Feel free to choose that below. This will require a page refresh to take effect.

About

"utk.claranguyen.me" details

Domain Name: claranguyen.me
Site Version: 3.0.1
Last Updated: 2019/08/18
File Information

PBM, PGM, and PPM files are all image file formats that hold data in regards to pixels of an image. Compared to formats like PNG, JPG, etc, these formats are very simplistic, offering no compression. These are simple formats that store the colours of pixels as bytes which can be read into your program.

There are three types:

  • PBM (Portable BitMap) - 2 colours only. Black and White (0-1)
  • PGM (Portable GrayMap) - 255 colours only. Black-Grey-White (0-255)
  • PPM (Portable PixMap) - 16,777,216 colours. Coloured RGB (0-255, 0-255, 0-255)
Each of these files also contain a magic number (Wikipedia) which tells if the information is stored as text or as bytes. We will cover this later in the documentation.

What these files look like (PPM Text Format)

Let's get directly into the details. For this example, we will use PPM files since they are the most used. Try opening a PPM file in a text editor of your choice (as opposed to an image viewer). If the format of the PPM file is stored as numbers, you will likely see this garbage:

PPM Image
P3
3 2
255
255   0   0     0 255   0     0   0 255
255 255   0   255 255 255     0   0   0
This is a 3x2 image that I stole from Wikipedia (It was public domain, hah). As it is 3x2, it is a very small image. Here is what it looks like when scaled up:

Breaking it down (PPM Text Format)

Let's look at that last file again. I will colour code the first 3 lines.

PPM Image
P3
3 2
255
255   0   0     0 255   0     0   0 255
255 255   0   255 255 255     0   0   0
  • P3 - Magic Number (Tells the program this is a PPM file)
  • 3 - Width
  • 2 - Height
  • 255 - Colour Range (0 = Black, This Number = White)
Everything beyond these numbers is pixel information about the image. Let's get to that.
PPM Pixel Data
255   0   0     0 255   0     0   0 255
255 255   0   255 255 255     0   0   0
We all have used Microsoft Paint before, at least I hope we have. We know the image has a size of 3x2. Since this is also a PPM file (Since the "P3" magic number was there), we can read the file accordingly. A PPM file stores 3 numbers per pixel. The first is the RED value, followed by the GREEN value, and then finally the BLUE value.

In relation to the data, let's colour code it:

255   0   0     0 255   0     0   0 255
255 255   0   255 255 255     0   0   0
Therefore, the first pixel has an RGB value of (255, 0, 0), which is literally red. The second pixel has an RGB value of (0, 255, 0), which is literally green. And so forth. Looking back at our image above, that is pretty accurate.

Here are some common colours you should know on the top of your head:

Name Red Green Blue Preview
Black 0 0 0
 
Gray 127 127 127
 
Red 255 0 0
 
Green 0 255 0
 
Blue 0 0 255
 
Yellow 255 255 0
 
Magenta 255 0 255
 
Cyan 0 255 255
 
White 255 255 255
 

In relation to the image:


Name Red Green Blue Preview
Red 255 0 0
 
Green 0 255 0
 
Blue 0 0 255
 
Yellow 255 255 0
 
White 255 255 255
 
Black 0 0 0
 

These are very simple colours and I am barely breaking the ice with this one. However, you should be getting the concept by now.

Text Format vs. Binary Format?

Up to this point, I have been generous to you, showing examples with a very simple image file of text only. However, realistically, computers read things differently. They like binary compared to numbers and text. Let's look at the file above one more time:

PPM Image
P3
3 2
255
255   0   0     0 255   0     0   0 255
255 255   0   255 255 255     0   0   0
This is the text format of PPM. Notice how the numbers after the "255" colour range identifier are human-readable numbers. What determines whether or not a program should read a file like this as binary or text is its magic number at the very top of the file. Here is a table of how the magic number works for PBM, PGM, and PPM files:

Magic Number File Type Extension Type
P1 Portable BitMap PBM ASCII
P2 Portable GrayMap PGM ASCII
P3 Portable PixMap PPM ASCII
P4 Portable BitMap PBM Binary
P5 Portable GrayMap PGM Binary
P6 Portable PixMap PPM Binary
P7 Portable ArbitraryMap PAM Unknown

In there, you can clearly see that P3 is defined as a PPM file that is Text-based (ASCII). However, there are other variants. P6 is the binary version of PPM.

"So Clara, what does a binary PPM file look like?"

I'm glad you asked. Chances are that your web browser can't even support me showing it here in raw binary... so I'll just show you a hex dump of it:

Hex Dump (6colour_ppmb.ppm)
00000000  50 36 0a 33 20 32 0a 32  35 35 0a ff 00 00 00 ff  |P6.3 2.255......|
00000010  00 00 00 ff ff ff 00 ff  ff ff 00 00 00           |.............|
0000001d
As you can see, the first 3 lines are readable text (line break = 0x0A). Notice the magic number is P6. This isn't that bad either to be honest. Since the file is binary, we can store the information on a colour in 3 bytes as opposed to the text format. One byte for red, one for green, and one for blue.

"Right... how do I read it?"

Take a look at this byte coloured in red...

Hex Dump (6colour_ppmb.ppm)
00000000  50 36 0a 33 20 32 0a 32  35 35 0a ff 00 00 00 ff  |P6.3 2.255......|
00000010  00 00 00 ff ff ff 00 ff  ff ff 00 00 00           |.............|
0000001d
This is the byte that comes right after the "255" in the file, and is a newline character. The colour data for each pixel exists right after it, starting at 0xFF. Here's it colour coded:
Hex Dump (6colour_ppmb.ppm)
00000000  50 36 0a 33 20 32 0a 32  35 35 0a ff 00 00 00 ff  |P6.3 2.255......|
00000010  00 00 00 ff ff ff 00 ff  ff ff 00 00 00           |.............|
0000001d

"Why is this a big deal? Who cares?"

In the text format, if you tried to store the number "100", you'd be using 3 bytes for that single number. Along with that, you'd be using bytes for spacing too between each number. The result is that each text file is usually around 2-4x larger than their binary variants, and takes longer to load. The only benefit you get out of it is that it's easier to read with your own eyes, which doesn't matter to the computer. You tell me which is superior. I think the answer is obvious.

Don't believe me? Here's a file size comparison:

UNIX Command
UNIX> fa
DIRECTORY: .
DIRECTORY COUNT: 0

FILE COUNT: 2
  -rwxr-xr-x 1 96 6colour_ppma.ppm
  -rwxr-xr-x 1 29 6colour_ppmb.ppm
For the text variant, the size is 96 bytes. For the binary variant, the size is 29 bytes. And they both store the same exact information.

In a more realistic sense, my phone shoots a picture at a resolution of 4032x3024. That's 12,192,768 pixels. If converted to a Binary PPM, it'd be around 36 megabytes. If converted to a Text PPM, it'd be (at max) around 146 megabytes. Binary is important.

Guide Information
Basic Information
Name: PBM, PGM, and PPM files
Description: The simple image file formats
ID: ppm
File Information
File Size: 15.48 KB (15856 bytes)
Last Modified: 2019/06/24 19:34:22
Version: 2.0.0
Translators
en-gb Clara Nguyen (iDestyKK)