CS074: The Digital World
Lab 3 Solutions
Due Wednesday, February 11
1. Create an image that is all black except for a white horizontal line
across the center.
Let's settle on an image size
first. We'll use the still-life that illustrates this assignment,
which is 144 pixels wide by 108 pixels high.
To make an all-black image, we first
set all cells to 0.
Let's make our white horizontal line
three pixels wide. So we want the pixels in rows 53 to 55 to be
set to 255. The first 52 rows take
up 52x3x144=22464 bytes, and the next
three rows take up 3x3x144=1296 bytes. So we will set all the
bytes with row numbers in the range 22464 to 22464+1295=23759 to 255,
and save the result as an image 144 pixels wide. Here is the
result.
2. Create an image that is all black except for a white vertical line
across the center. (If you're a bit off in your calculations,
your line might be a little ragged-looking, because it does not exactly
fill the rows, or not quite centered).
Now let's set columns 71-73 to
255. We'll begin by deleting everything after the first row
(every byte number 144x3=432 or higher), then setting bytes 210 to 218
to 255.
We then take this range of bytes and
repeatedly copy and paste, so until we wind up with enough rows.
I doubled seven times to get 128 rows. Save the result as
an image with width 144.
Here is the result.
3. Create a grayscale negative of a color image you find on the
Web. (Don't use the one displayed in these examples.)
After loading the image, first create
a grayscale version as we did before. Then subtract every byte
value from 255.
4. Create a black and white monochrome version of a color image that
you find on the Web.
First create a grayscale image. Then
set every byte with value between 128 and 255 to 255, then every byte
with value between 0 and 127 to 0.
5. (a little harder) Increase the contrast of an
image. An extreme version of this is one-bit-per-color image
shown above (the third image on this page). A more moderate
version would set, say, every byte value less than 64 to 0, every byte
value greater than 192 to 255, and replace each intermediate byte value
b by 2 x (b-64). This has the effect of making the light areas
lighter and the dark areas darker. If you want, you can
experiment with other values. You can also try to decrease the
contrast.
It would have been harder if I hadn't
given you this algorithm! So first set every byte with value
between 0 and 63 to 0, and then every byte with value between 193 and
255 to 255. Then decrease every byte with value between 64 and 192 by
64, and multiply every byte with value between 1 and 128 by 2.
Here is the result.
6. This exercise requires you to use a fairly small image, no more than
200 x 200 pixels, since the Select operation takes too long with a
large one. It's better also if you make sure that the width is a
multiple of 3. Take the color image, convert it to grayscale as above,
and then select every third value. Before you try to display it,
think hard about what will happen if you (a) display it at the original
width of the image; (b) display it at one-third the original
width. Then see what actually happens. Try to explain the
result.
Here are the results.
When we display at the original width,
every row of the displayed image comes from three successive rows of
the original image. So the three small copies of the image are
not quite identical, though it's difficult to see this. Nor are
they entirely gray: Each block of three bytes now comes from
three different pixels, so there may be some color. However the
three bytes will probably be very close in value, so the picture does
look pretty gray.
When we display at one-third the
original width, each row of the displayed image now consists of data
from one row of the original image, which has the effect of squashing
the picture in the horizontal direction by a factor of three. Again, in
principle this is not entirely gray, although it looks pretty gray!
.