Functional Problem Solving (CSC 151 2014F) : Labs

Laboratory: Simple Side Effects with Text and Images


Summary: In this laboratory, you will explore procedures that have side effects, particularly procedures that generate textual output or manipulate images. Along the way, you should reflect on the differences between the original “just compute a result” model, and this new model.

Preparation

If you haven't already, make a copy of side-effects-lab.rkt, which contains the code from the readings.

Exercises

Exercise 1: Showing and Closing Images

The reading on basic image operations suggests that what you can do with images depends a bit on what you've done with those images in Gimp.

You'll note that the code for this lab includes three drawings, red-square, blue-circle, and black-rectangle.

a. Predict what will happen when you enter the following instructions in the interactions pane.

> (define canvas (image-new 100 100))
> (image-show canvas)
> (drawing-render! red-square canvas)
> (image-show canvas)
> (drawing-render! blue-circle canvas)
> (image-show canvas)

b. Check your prediction experimentally.

c. Close all of the images created in steps a and b.

d. Predict what will happen when you enter the following instructions in the interactions pane.

> (drawing-render! black-rectangle canvas)
> (image-show canvas)

e. Check your prediction experimentally.

f. Summarize with your partner what you learned from this exercise.

Exercise 2: Loading Images

a. Find an image, such as your campus directory picture, and save it to your desktop.

b. Using image-load, image-show, and anything else that you think will be useful, load and show that image. Note that the path to the file is likely to be something like "/home/username/Desktop/image.jpg", although you will need to replace the user name and image.

c. Using image-load, image-show, and anything else you think will be useful, load and show "/home/rebelsky/glimmer/samples/rebelsky-pic.jpg".

d. Using drawing-render!, add one of the simple drawings to that image.

e. Using image-save, save the modified image to your desktop.

f. Try opening the modified image by double-clicking on it.

g. You probably don't need the image any more. Delete it.

Exercise 3: Exploring the Creation of Drawings

In the first few problems, we explored ways in which we can see how Scheme evaluates numeric expressions. Wouldn't it be equally nice to see what happens as we create complex drawings?

a. Write annotated versions of hscale-drawing, vscale-drawing, scale-drawing, hshift-drawing, vshift-drawing, and recolor-drawing (started in the provided code) that print out a short description as they run.

Here's an example to get you started.

(define annotated-hscale-drawing
  (lambda (scale drawing)
    (display "Scaling ")
    (display drawing)
    (display " horizontally by ")
    (display scale)
    (yield (hscale-drawing scale drawing))))

b. Predict the image that results from the following instructions.

(define d0 (scale-drawing 50 drawing-unit-circle))
(define d1 (vscale-drawing 6/5 d0))
(recolor-drawing "red" d0)
(scale-drawing 10 d0)
(recolor-drawing "blue" d1)
(hshift-drawing 20 d1)
(define d2 (drawing-group (hshift-drawing 30 d1)
                          (vshift-drawing 20 d1)))
(image-show (drawing->image d2 100 100))

c. Check your answer experimentally.

d. Replace each of the calls above with their annotated versions and see what seems to be happening.

Exercise 4: Scaling Drawings

Try the scare! procedure from the reading to scale and render the unit circle on the following images.

  • a. On new 100x100, 200x100, and 100x200 images.
  • b. On the image you saved on the desktop in an earlier exercise.
  • c. On an image created by drawing->image.

For those with Extra Time

If you find that you have extra time, you might want to consider doing the extra problems, which emphasize algorithms and programming, or the explorations, which emphasize design.

Extra 1: Exploring Order of Evaluation, Revisited

Right now, we get a lot of output from these annotated procedures. For example,

> (add 2 4)
add 2 and 4
  yields 6

Rewrite the procedures so that we get much more concise, one-liner, output in normal mathematical form. For example,

> (add 2 4)
2 + 4 = 6

Extra 2: Scaling Drawings, Revisited

You may have noted that when we use scare! with the unit circle, our circle (or oval) remains centered at (0,0). Rewrite scare! so that the left edge of the drawing is shifted to the left edge of the image and the top edge of the drawing is shifted to the top edge of the image. (You may find it helpful to write one or more helper procedures.)

Make sure that your new scare! procedure works for each of the following drawings.

(define d1 drawing-unit-circle)
(define d2 (recolor-drawing "red" (hshift-drawing 0.5 d1)))
(define d3 (recolor-drawing "blue" (hshift-drawing -0.5 d1)))
(define d12 (drawing-compose d1 d2))
(define d13 (drawing-compose d1 d3))
(define d23 (drawing-compose d2 d3))

Extra 3: Claiming Images

a. Verify that the claim procedure works as advertised.

b. Redesign the icon and see if the redesigned icon can also be attached to images.

Explorations

As the claim procedure implies, there is a genre in which artists appropriate others' works, make some modifications to them, and claim them as their own. Duchamp's L.H.O.O.Q is an early example of this approach, and Sherrie Levine's After Walker Evans shows a form of appropriation without obvious modification.

Design a procedure that appropriates other images in a way that you find interesting. You might wish to follow the model of claim and just put a small icon somewhere on the image, or you might choose to put larger drawings throughout the image. For example, you might just leave one vertical section of the image showing behind two large rectangles.