How Computers Represent ImagesThe PPM File Format(http://cs.anu.edu.au/Student/comp1100.2006.s1/assts/asst1/ppm.html)

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.


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值