## 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.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.1.2 we give first examples.

In Figure 12.1.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.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.1.3 and Figure 12.1.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.1.3 and Figure 12.1.4 column (b).

#### (c)

We consider the digits in each line of the raster as the digits of a binary number. Compare Figure 12.1.3 and Figure 12.1.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.1.3 and in Figure 12.1.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.1.3 we get that the image is represented by the decimal numbers

In the example in Figure 12.1.4 we get that the image is represented by the decimal numbers

see Figure 12.1.4 (e).

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.1.5 we apply this method to the example from Figure 12.1.3 .

In Figure 12.1.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.

### Problem 12.1.8. Represent image by numbers.

Represent the image from Figure 12.1.7.(a) by a decimal number for each row. In the bitmap, use 1s to represent black pixels.

As the binary representations for the rows of the raster we obtain:

Now, we must convert these numbers from base \(2\) to base \(10\) representation by writing out their base \(2\) expansions and evaluating them

So, the representation of the image by decimal numbers is \(7\) , \(4\) , \(6\) , \(4\) , \(4\) .

In Checkpoint 12.1.9 represent an image by a sequence of decimal numbers.

### Checkpoint 12.1.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.1.10. Image represented by numbers.

Which of the images in Figure 12.1.7 can be encoded as \(1\) , \(1\) , \(3\) , \(1\) , \(7\) , when \(1\) represent a black pixel and \(0\) represents white ?

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:

To help us more completely visualize the image, we insert leading 0s:

Now, we see that the decimal numbers 1,1,3,1,7 encode Figure 12.1.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.1.11 is such a problem.

### Checkpoint 12.1.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

3

2

2

\text{}

What is the letter ?