Skip to main content
Logo image

Section 12.1 Images

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 .
Figure 12.1. For the game Ant Attack (by Sandy White, 1983) graphics were designed on squared paper, each filled square taken as a binary \(1\) , each empty square as \(0\) , converted into hexadecimal representation and then typed in, see http://sandywhite.co.uk/fun/ants . You can play Ant Attack at http://torinak.com/qaop#!antattack . Hint: v run, c jump, m turn left, , turn right, s d f g grenade, 0 p space enter change view
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.
Figure 12.2. Applications of Other Bases (Image to Numbers)by Matt Farmer and Stephen Steward
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.

Strategy 12.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.
As examples we process the image given in Figure 12.3 and Figure 12.4 column (a).

(b)

We represent white pixels by 0s and black pixels by 1s and fill in the cells of the raster accordingly. We call this version of the raster a bitmap .
This step is illustrated in Figure 12.3 and Figure 12.4 column (b).

(c)

We consider the digits in each line of the raster as the digits of a binary number. Compare Figure 12.3 and Figure 12.4 column (c).

(d)

Since leading zeros do not change the value of numbers in any base, we can remove the leading zeros of the binary numbers.
In our examples in Figure 12.3 and in Figure 12.4 we obtain the binary numbers listed in column (d).

(e)

We convert the binary numbers into decimal numbers.
In the example in Figure 12.3 we get that the image is represented by the decimal numbers
\begin{equation*} 6,10,7,13 \end{equation*}
In the example in Figure 12.4 we get that the image is represented by the decimal numbers
\begin{equation*} 0, 8, 56, 84, 108, 56, 108, 0, 0 \end{equation*}
see Figure 12.4 (e).
described in detail following the image
Table with four columns, labeled (a) image, (b) bitmap, (c) binary (with leading 0s), (d) binary, (e) decimal.
The first column contains a four by four raster with black and white pixels. The second column contains a four by four raster in which cells that were black in the first column contain ones and the positions that were white in the first column contain zeros. The third and fourth column contain four numbers in binary representation and the fifth column contains four numbers in decimal representation.
First row: the pixels in the raster in the first column are white, black, black, white; the entries in the raster in the second column are zero, one, one, zero, the binary number with leading zeros in the third column is zero one one zero, the binary number in the fourth column is one one zero, the decimal number in the fourth column is six. Second row: the pixels in the raster in the first column are black, white, black, white; the entries in the raster in the second column are one, zero, one, zero, the binary number with leading zeros in the third column is one zero one zero, the binary number in the fourth column is one zero one zero, the decimal number in the fourth column is ten. Third row: the pixels in the raster in the first column are white, black, black, black; the entries in the raster in the second column are zero, one, one, one, the binary number with leading zeros in the third column is zero one one one, the binary number in the fourth column is one one one, the decimal number in the fourth column is seven. Fourth row: the pixels in the raster in the first column are black, black, white, black; the entries in the raster in the second column are one, one, zero, one the binary number with leading zeros in the third column is one one zero one, the binary number in the fourth column is one one zero one, the decimal number in the fourth column is thirteen.
Figure 12.3. Encoding the image on the left, as a bitmap (black pixels are 1s, white pixels are 0s), in binary, and in decimal representation. Each number represents one row in the image.
Figure 12.4. Encoding the image on the left, as a bitmap (black pixels are 1s, white pixels are 0s), in binary, and in decimal representation. Each number represents one row in the image.
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 .
described in detail following the image
Table with two columns, the first columnn is labeled image and the second column is labeled computation and decimal representation.
The first column contains a four by four raster with black and white pixels. The clumns of the raster, from left to right, are labeled 1, 2, 4, 8.
First row: the pixels in the raster are white, black, black, white; the computation and decimal representation is 4+2=6. Second row: the pixels in the raster are black, white, black, white; computation and decimal representation is 8+2=10. Third row: the pixels in the raster are white, black, black, black; computation and decimal representation is 4+2+1=7. Fourth row: the pixels in the raster are black, black, white, black; computation and decimal representation is 8+4+1=13.
Figure 12.5. Encoding the image on the left, as a bitmap (black pixels are 1s, white pixels are 0s), by writing the place values of the binary digits over the columns of the raster. Recall that the place values of a four digit binary number from right to left are \(2^0={\color{blue}1}\) , \(2^1={\color{blue}2}\) , \(2^2={\color{blue}4}\) and \(2^3={\color{blue}8}\) .
In Figure 12.6 observe how the representation of the rows of the image on the left changes as you change the image.
Figure 12.6. Image to numbers interactive. Click on the rectangles on the left to toggle between black and white. Each row is converted to a binary number where \(0\) represents white and \(1\) represents black and to decimal representation. The powers of two for each column are given in blue. Recall that
\begin{equation*} 2^7=128,\; 2^6=64,\;2^5=32,\;2^4=16,\;2^3=8,\;2^2=4,\;2^1=2,\;2^0=1 \end{equation*}
We continue with some more examples.
described in detail following the image
Three by five raster whose pixel are either black or white.
First row: black, black, black. Second row: black, white, white. Third row: black, black, white. Fourth row: black, white, white. Fifth row: black, white, white.
(a)
described in detail following the image
Three by five raster whose pixel are either black or white.
First row: black, black, black. Second row: white, white, black Third row: white, black, black. Fourth row: white, white, black. Fifth row: white, white, black.
(b)
described in detail following the image
Three by five raster whose pixel are either black or white.
First row: black, white, white. Second row: black, white, white. Third row: black, black, white. Fourth row: black, white, white. Fifth row: black, black, black.
(c)
described in detail following the image
Three by five raster whose pixel are either black or white.
First row: white, white, black. Second row: white, white, black. Third row: white, black, black. Fourth row: white, white, black. Fifth row: black, black, black.
(d)
Figure 12.7. Images for Problem 12.8 and Problem 12.10

Problem 12.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:
\begin{equation*} 111_2,\,100_2,\,110_2,\,100_2,\,100_2 \end{equation*}
Now, we must convert these numbers from base \(2\) to base \(10\) representation by writing out their base \(2\) expansions and evaluating them
\begin{align*} 111_2 \amp = 1 \cdot 2^2 + 1 \cdot 2 + 1 \cdot 1 = 7\\ 100_2 \amp = 1 \cdot 2^2 + 0 \cdot 2 + 0 \cdot 1 = 4\\ 110_2 \amp = 1 \cdot 2^2 + 1 \cdot 2 + 0 \cdot 1 = 6\\ 100_2 \amp = 1 \cdot 2^2 + 0 \cdot 2 + 0 \cdot 1 = 4\\ 100_2 \amp = 1 \cdot 2^2 + 0 \cdot 2 + 0 \cdot 1 = 4 \end{align*}
So, the representation of the image by decimal numbers is \(7\) , \(4\) , \(6\) , \(4\) , \(4\) .
In Checkpoint 12.9 represent an image by a sequence of decimal numbers.

Checkpoint 12.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.

Problem 12.10. Image represented by numbers.

Which of the images in Figure 12.7 can be encoded as \(1\) , \(1\) , \(3\) , \(1\) , \(7\) , 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:
\begin{align*} 1_{10} \amp = 1_2\\ 1_{10} \amp = 1_2\\ 3_{10} \amp = 11_2\\ 1_{10} \amp = 1_2\\ 7_{10} \amp = 111_2 \end{align*}
To help us more completely visualize the image, we insert leading 0s:
\begin{align*} 1_{10} \amp = 001_2\\ 1_{10} \amp = 001_2\\ 3_{10} \amp = 011_2\\ 1_{10} \amp = 001_2\\ 7_{10} \amp = 111_2 \end{align*}
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.

Checkpoint 12.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.
2
7
2
2
3
What is the letter ?