For computers to display graphics, the graphics need to be represented in a format that a computer can process, that is, as numbers. In this section we consider the easiest case, namely black and white images that are given in a raster of pixels and then converted into a sequence of numbers. For early computer games this process was done by hand as illustrated in Figure 12.1.
We describe how an image that consists of pixels (the little rectangles that are the points in a raster image) can be encoded into numbers. In the video in Figure 12.2 we give first examples.
In Figure 12.4 the image on the left is converted to the decimal numbers given on the right. We describe the steps in detail.
Strategy12.1.Image to numbers.
To convert an image to a list of decimal numbers we proceed as follows.
(a)
We start with an image that consists of black and white pixels.
We can combine the steps described above into one step.
First, we write the powers of two from right to left over the columns of the image. To compute the decimal representation of each row, add the numbers you wrote over the columns for which there is a black pixel in the row. In Figure 12.5 we apply this method to the example from Figure 12.3.
In Figure 12.6 observe how the representation of the rows of the image on the left changes as you change the image.
We continue with some more examples.
Problem12.8.Represent image by numbers.
Represent the image from Figure 12.7.(a) by a decimal number for each row. In the bitmap, use 1s to represent black pixels.
Solution.
As the binary representations for the rows of the raster we obtain:
So, the representation of the image by decimal numbers is \(7\text{,}\)\(4\text{,}\)\(6\text{,}\)\(4\text{,}\)\(4\text{.}\)
In Checkpoint 12.9 represent an image by a sequence of decimal numbers.
Checkpoint12.9.Represent image by numbers.
0
0
0
0
1
0
0
1
0
0
1
0
0
1
1
0
1
0
0
0
0
1
0
1
1
0
0
1
0
0
1
0
1
0
0
1
Represent each row of the bitmap by a decimal number. When converting to decimal let the most significant binary digit be on the left.
When recovering an image from its representation by a sequence of decimal numbers, we first convert the decimal numbers to binary representation and then filling in the pixels. Usually writing the binary numbers with leading zeros so that they all have the same length gives a good idea what the image looks like.
Problem12.10.Image represented by numbers.
Which of the images in Figure 12.7 can be encoded as \(1\text{,}\)\(1\text{,}\)\(3\text{,}\)\(1\text{,}\)\(7\text{,}\) when \(1\) represent a black pixel and \(0\) represents white ?
Solution.
We must work in the opposite direction from the previous problem to answer this question. The decimal numbers are given. First, we change those decimal numbers into binary numbers:
Now, we see that the decimal numbers 1,1,3,1,7 encode Figure 12.7.(d).
In problems that ask you to recover an image from its decimal representation, we often ask you which letter is shown in the image. Checkpoint 12.11 is such a problem.
Checkpoint12.11.Letter in image represented by numbers.
An image showing a letter is encoded into numbers.
In the encoding a 0 corresponds to a white and a 1 to a black pixel. When converting to decimal the most significant binary digit was on the left.