Colours

The colours you see on a computer screen are made up of tiny red, green and blue dots. To specify a colour, you have to say how much red, how much green, and how much blue is in the colour, and the usual range is 0 .. 255 for each value. Here's some examples of colours and the RGB (red green blue) values that go with them:

White: R is 255, G is 255, B is 255
Red: R is 255, G is 0, B is 0
Orange: R is 255, G is 153, B is 62
Yellow: R is 255, G is 255, B is 0
Green: R is 0, G is 255, B is 0
Cyan: R is 0, G is 255, B is 255
Blue: R is 0, G is 0, B is 255
Purple: R is 193, G is 0, B is 159
Black: R is 0, G is 0, B is 0


Pixels and Images

Computers represent images by a grid of tiny coloured dots, called pixels. (The word "pixel" is an abbreviation of "picture element".) The pixels are so small that, when displayed on a computer screen, they appear to merge into a smooth image.

Here is an example of a very small image 4 pixels wide and 3 pixels high, along with an enlargement:

[demo image]

Here is an image which is 300 pixels wide and 184 pixels high:

The colour of each pixel in an image is typically specified by giving its RGB values.


The PPM File Format

In order to store an image, the computer doesn't merely store the colours of the pixels. It has to choose which image format to store it in. All image formats have this same general sort of structure:

Some information to say which sort of image format this is
Some header information (including width and height of image)
Pixel data (usually compressed)

PPM stands for "Portable Pix Map". It is a very old image format, that can represent any ordinary colour image. PPM files are plain text files with uncompressed image data, which means that it is very easy for a human to read the file.

Let's have a look at an actual PPM file, using the sample image

This is the PPM file representing that image:

    P3
# Created by CB-F using emacs
4 3
255
0 0 0 0 0 0 0 0 0 255 0 0
0 0 0 255 255 255 255 0 0 150 150 255
0 0 0 255 0 0 150 150 255 150 150 255

  • The P3 means that the file is a PPM file
  • the # Created by CB-F using emacs is a comment
  • the next line 4 3 gives the width and height of the file
  • the line after that 255 gives the range of values that the colours can take (0 .. 255)
  • All the numbers after that are the pixel data, starting from the top left and moving along each row, row by row. So, for example, the first 0 0 0 refers to the top left pixel having RGB values of (0,0,0), black. The last RGB values are 150 150 255, which refer to the bottom right pixel having RGB values of (150,150,255), a blue colour.

The pixel data in the file is uncompressed (and is text numerals rather than numbers), and therefore it takes up large amounts of memory. For example, the fireworks image above takes up 8K as a JPG image (JPG is another image format), but 176K as a PPM file. So PPMs have the advantage that it's easy to work with the pixel data (you can read it and it's easy to extract from the file) but they take up a lot of space. So when you are writing programs that manipulate PPMs, use small images to test your programs.


 
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐