## Section12.1Images

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. Figure 12.1.1. For the game Ant Attack (by Sandy White, 1983) graphics were designed on squared paper, each filled square taken as a binary $$1\text{,}$$ each empty square as $$0\text{,}$$ 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.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.

### Strategy12.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

\begin{equation*} 6,8,7,13 \end{equation*}

In the example in Figure 12.1.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.1.4 (e). Figure 12.1.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.1.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.1.5 we apply this method to the example from Figure 12.1.3. Figure 12.1.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}\text{,}$$ $$2^1={\color{blue}2}\text{,}$$ $$2^2={\color{blue}4}$$ and $$2^3={\color{blue}8}\text{.}$$

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.

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.

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\text{,}$$ $$4\text{,}$$ $$6\text{,}$$ $$4\text{,}$$ $$4\text{.}$$

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

Select the colors that best describe the colors represented by the RGB hex triplets.

1. $$\displaystyle \#\mathtt{111111}$$

2. $$\displaystyle \#\mathtt{A0A0A0}$$

3. $$\displaystyle \#\mathtt{00FFFF}$$

4. $$\displaystyle \#\mathtt{FFFFFF}$$

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.

Which of the images in Figure 12.1.7 can be encoded as $$1$$ ,$$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:

\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.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.

Select the RGB hex triplets that correspond to the given colors.

1. $$\color{black}{\blacksquare}$$ black

2. $$\color{red}{\blacksquare}$$ red

3. $$\color{cyan}{\blacksquare}$$ cyan

4. $$\color{yellow}{\blacksquare}$$ yellow