# Algorithmic Art

Summary: In this laboratory, you will continue your exploration of Script-Fu by using more Scheme-like operations to automatically generate art. You will also explore techniques for modifying existing images.

## Exercises

### Exercise 0: Preparation

a. Start the Gimp.

b. Open the Script-Fu console.

c. Open the Script-Fu procedure reference.

d. Make a copy of `art.scm`, the sample code for this lab.

e. Start DrScheme.

f. Open your copy of `art.scm` in DrScheme (so that you may modify it).

g. Load your copy of `art.scm` in the Script-Fu console (so that you may use it).

### Exercise 1: Higher-Order Drawing

We've seen a number of ways in which Scheme can be useful for creating images. But we haven't yet used all the wonders of Scheme. For example, we have yet to take advantage of recursion or higher-order procedures. I've written a simple procedure, `(gsfu-create-image width height func)`, that creates a new image and layer by applying func to every pair of (x,y) values in a new image.

Here's a sample procedure you might use:

```(define exercise-one
(lambda (x y)
(list
(mod (+ x y) 256)
(mod (* y y) 256)
(mod (trunc (* 1000 (sin x))) 256))))
```

a. Try to create a small image (say 100 by 100) using `gsfu-create-image` and `exercise-one`.

Note that the list returned contains an image id and a layer id. You'll need to show the result using `(gimp-display-new image-id)`.

b. Try to create a somewhat larger image (say 200 by 150) using `gsfu-create-image` and `exercise-one`.

c. What, if anything, do you note about the relationship of the two images?

### Exercise 2: Your Own Functions

Experiment with you own variations of `exercise-one`. You might try using `sin`, `cos`, powers, addition, subtraction, and much much more.

Warning: A 200 by 150 image has 300,000 locations. If each step takes a lot of time (e.g., because it uses lots of complicated procedures), drawing will take a very long time!

Things can get even more interesting when you work with an existing graphic. It's fairly easy to load and get information about an existing graphic. You can load an image file with

```(gsfu-load-image path)
```

For example,

```(gsfu-load-image "/home/rebelsky/Web/CS151/2003S/Examples/povilas.jpg")
```

(Yes, there are also built-in commands for loading images. Feel free to look for them in the procedure browser. I find mine easier to use.)

Try loading an image of your choice using `gsfu-load-image`. Note that the list returned contains an image id and a layer id. You'll need to show the result using ```(gimp-display-new image-id)```.

### Exercise 4: Mapping Images

You may recall that we used the `map` procedure to build a list by applying a function to every value in another list. It is similarly possible to map a function onto an image by getting the color at each point, applying the function, and then setting the color to the result of the function.

I've implemented a map for images as

```(gsfu-map-image image layer func)
```

This procedure returns a list of the id of the new image and the id of the layer in that new image.

a. Here's a simple procedure that transforms a color onto another color.

```(define four-a
(lambda (color)
(let* ((red (car color))
(list green blue red))))
```

What do you think will happen if you run `gsfu-map-image` using this procedure?

Verify your results experimentally. Note that it may take a minute or two for the procedure to run. You may want to read ahead while waiting for `gsfu-map-image` to complete.

b. Here is another simple procedure that maps a color onto another color.

```(define four-b
(lambda (color)
(let* ((red (car color))
(tmp (/ (+ red green blue) 3)))
(list tmp tmp tmp))))
```

What do you think will happen if you run `gsfu-map-image` using this procedure?

### Exercise 5: Mapping Images, Revisited

Rather than applying a function to a color at each position, we might want to compute other aspects of the result based on the position. For example, we might want to base each pixel in the destination image on a pixel in a different location in the source image.

The procedure ```(gsfu-map-coordinates image layer draw-proc)``` creates a new image by applying the procedure `draw-proc` for every (x,y) pair in the image. The `draw-proc` procedure takes eight (8) parameters:

• source-image, The id of the source image
• source-layer, The id of the source layer
• dest-image, The id of the destination image
• dest-layer, The id of the destination image
• width, The width of both images
• height, The height of both images
• x, the x position to be processed
• y, the y position to be processed

a. For example, here's a procedure you might use as the draw-proc parameter to gsfu-map-coordinates:

```(define five-a
(lambda (source-image source-layer dest-image dest-layer
width height x y)
(gsfu-set-color dest-layer x y
(gsfu-get-color source-layer x (mod (+ x y) height)))))
```

What effect do you expect this procedure to have?

b. Here's another interesting procedure.

```(define five-b
(lambda (source-image source-layer dest-image dest-layer
width height x y)
(gsfu-set-color dest-layer x y
(gsfu-get-color source-layer
(trunc (* width (sqrt (/ x width))))
y))))
```

What effect do you expect this procedure to have?

## For Those with Extra Time or Interest

You do not need to do these extra exercises in order. Rather, choose the ones that seem most interesting to you.

### Extra 1: Your Own Color Transforms

Write and test a few color transformations of your own with `gsfu-map-image`. Your transformations might include:

a. One that adds 128 to each color value and then mods by 256.

b. One that subtracts each color value from 256.

c. One that makes each new color value depend on a combination of color values in the earlier image.

d. One that uses `max` or `min` in some interesting way.

### Extra 2: Different Geometric Transforms

Design and test a few geometric transforms (like those from problem 5). Try more interesting combinations of x and y.

### Extra 3: Blurring

Write an instruction to blur an image by using `gsfu-map-coordinates` to average the values of surrounding pixels. (Warning! You should try this on a very small image.)

### Extra 4: Combining Transforms

Using `gsfu-map-coordinates` write an instruction that transforms an image by doing something interesting with pixels at different locations in the picture.

## History

Friday, 1 November 2002 [Samuel A. Rebelsky]

• Created.

Friday, 1 November 2002 [Samuel A. Rebelsky]

• Created section for those with extra time.
• Moved variations on `gsfu-map-image` to new section.
• Added new problems based on `gsfu-map-coordinates`.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue May 6 09:28:34 2003.
The source to the document was last modified on Fri Apr 4 13:27:43 2003.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003S/Labs/algorithmic-art.html`.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu