Laboratory: Storing Images with Pixmaps
Summary:
In this laboratory, you will further explore the use of pixmaps to
represent an image.
Preparation
Add the preliminary versions of rgb-write and
rgb-read to your definitions pane.
Exercises
Exercise 1: Reading and Writing Simple Colors
a. Write the three primaries (red, green, blue), the three secondaries
(cyan, magenta, and yellow), and black and white to the file
some-colors.txt. That is, open a port to that
file, write the colors using rgb-write, and then
close the port.
b. Open some-colors.txt and see if the numbers
make sense. (If you open some-colors.txt in
MediaScript, you'll need to make sure it's set to show all file types,
and not just scheme files.)
c. Using rgb-read, read back all the colors from the file,
and verify that you have the same colors. For example, after opening
the file and naming the port source, you might write
> (define color (rgb-read source))
> (rgb->color-name color)
> (rgb->string color)
Exercise 2: Reading and Writing Components
a. Update the definitions of rgb-write and
rgb-read so that they write each component
separately.
b. Write the three primaries, the three secondaries, and black and
white to the file
some-colors.txt.
c. Open some-colors.txt and see if the numbers
make sense.
d. Using rgb-read, read back all the colors from the file,
and verify that you have the same colors.
Exercise 3: Writing Code to Write Images
a. Create an interesting 4x3 image. If you don't want to figure out an
interesting image yourself,
you can use the following set of commands.
(define canvas (image-new 4 3))
(image-set-pixel! canvas 0 0 (rgb-new 0 0 255))
(image-set-pixel! canvas 0 1 (rgb-new 16 16 240))
(image-set-pixel! canvas 0 2 (rgb-new 32 32 224))
(image-set-pixel! canvas 1 2 (rgb-new 48 48 208))
(image-set-pixel! canvas 2 2 (rgb-new 64 64 192))
(image-set-pixel! canvas 3 2 (rgb-new 80 80 176))
(image-set-pixel! canvas 3 1 (rgb-new 96 96 160))
(image-set-pixel! canvas 3 0 (rgb-new 112 112 144))
b. Write that image to a file, using code similar to that
in the reading. Here's a start.
> (define pixmap (open-output-file "image1.pixmap"))
> (rgb-write (image-get-pixel canvas 0 0) pixmap)
> (rgb-write (image-get-pixel canvas 1 0) pixmap)
> (rgb-write (image-get-pixel canvas 2 0) pixmap)
> (rgb-write (image-get-pixel canvas 3 0) pixmap)
> (rgb-write (image-get-pixel canvas 0 1) pixmap)
> (rgb-write (image-get-pixel canvas 1 1) pixmap)
...
...
...
> (close-output-port pixmap)
c. What do you expect the file to contain?
d. Open the file you've created and verify that it has the form you
expected.
Exercise 4: Writing Images with image-write-pixmap
a. Add image-write-pixmap to your definitions
window. You can find the definition of this procedure at the end
of the lab.
b. Change the image from the previous exercise, using a command like
> (image-transform! canvas rgb-complement)
c. Using image-write-pixmap, write the modified
image to the file image2.pixmap.
d. What values do you expect that file to have?
e. Verify that the file contains the values you were expecting.
Exercise 5: Reading Images
a. Add image-read-pixmap to your definitions
window. You can find the definition of this procedure at the end
of the lab.
b. Clear your current canvas. In GIMP, you can select all and then
delete. You can also use the following commands, which do exactly the
same thing.
> (image-select-all! canvas)
> (image-clear-selection! canvas)
> (context-update-displays!)
c. Using image-read-pixmap!, fill in the canvas from
image1.pixmap
d. Clear the canvas again.
e. Using image-read-pixmap!, fill in the canvas from
image2.pixmap
Exercise 6: Reading Into Other Size Images
a. Create four new images ---
canvas6x2,
canvas3x4,
canvas2x6, and
canvas12x1 ---
whose sizes match their names.
b. What do you expect to have happen if we read from
image1.pixmap into each of these images?
c. Check your answer experimentally.
d. Create two new images,
canvas20x1 and canvas3x3,
whose sizes match their names.
e. What do you expect to have happen if we read from
image1.pixmap into each of these images?
f. Check your answer experimentally.
Exercise 7: Including the Image Size
As the previous exercise suggests, one problem with the way we've
chosen to store images in files is that we lose the size of the image.
But without the size, the representation is not sufficient to restore
the image. What can we do? We can first write the width and height of
the image to the file.
a. Rewrite image-write-pixmap so that it first
writes the width and the height of the image to the file.
b. Rewrite image-read-pixmap! so that it verifies
that the image stored in the file is the same size as the given image.
c. Write a new procedure, (image-read-pixmap
filename), the takes as a parameter only
a file name, reads the image size from the file, creates a new image
of the appropriate size, and fills in the image from the file.
Exercise 8: Writing Pixmaps By Hand
a. Create a file, image3.pixmap, with the following
contents.
4
3
255 0 0
255 128 0
128 255 0
0 255 0
128 0 128
128 128 128
128 192 128
128 255 128
0 0 255
128 128 255
192 192 255
255 255 255
b. What do you expect to have happen if you load that file with
your newly-written image-read-pixmap?
c. Check your answer experimentally.
Explorations
Pick a reasonably small image of your choice and write it to a file with
image-write-pixmap.
See what effects you can obtain by reading that file into an image of
a very different shape.