Functional Problem Solving (CSC 151 2014F) : Labs

Laboratory: Transforming Images


Summary: In this laboratory, you will extend the operations you've used to transform colors into operations that transform images.

Reference:

(image-set-pixel! image column row rgb-color)
MediaScheme GIMP Procedure. Set the pixel at the specified position to the new color.
(image-variant image fun)
MediaScheme GIMP Procedure. Create a new image of the same width and height as image, each of whose pixels is computed by applying fun to the color of the corresponding pixel in image.
(image-transform! image fun)
MediaScheme GIMP Procedure. Transform image in place by setting each pixel to the result of applying fun to that current pixel color.
(compose f g)
Traditional Higher-Order Procedure. Build a one-parameter procedure that applies g to its parameter, and then f to that result. ((compose f g) x) is the same as (f (g x)).
(o f1 f2 ... fn-1 fn)
Traditional Higher-Order Procedure. Build a one-parameter procedure that applies each f, in turn, starting with fn and working backwards. The composition, when applied to a value, x, produces the same result as (f1 (f2 (... (fn-1 (fn x))))).
(irgb-lighter irgb-color)
MediaScheme Color Procedure. Build a lighter version of the given color.
(irgb-darker irgb-color)
MediaScheme Color Procedure. Build a darker version of the given color.
(irgb-redder irgb-color)
MediaScheme Color Procedure Build a redder version of the given color.
(irgb-greener irgb-color)
MediaScheme Color Procedure. Build a greener version of the given color.
(irgb-bluer irgb-color)
MediaScheme Color Procedure. Build a bluer version of the given color.
(irgb-rotate irgb-color)
MediaScheme Color Procedure. Rotate the three components of the given color, setting the red component to the value of the green component, the green component to the value of the blue component, and blue compnent to the value of the red component.
(irgb-phaseshift irgb-color)
MediaScheme Color Procedure.Phase shift” the color by adding 128 to components less than 128 and subtracting 128 from components greater than 128.
(irgb-complement irgb-color)
MediaScheme Color Procedure. Compute the psuedo-complement of the given color.
(image-transform-pixel! image column row func)
MediaScheme GIMP Procedure. Modify the pixel at (col,row) in image by applying func to its old color and setting that pixel to the resulting color.

Preparation

If you did not already do the preparation from the self-check in the reading, please do it now.

Exercises

Exercise 1: Pure Transformations, Revisited

As you may have observed, there are times that transforming an image can be “dangerous” because we cannot easily undo some transformations (at least not without the magic “Undo” menu item). In spite of this problem, many programmers still prefer side-effecting operations like image-transform!.

With your partner, come up with some reasons that it may be advantageous to use image-transform! rather than image-variant. Be prepared to share those reasons with your classmates.

Exercise 2: Composition

Consider the following definitions.

> (define much-darker (compose irgb-darker irgb-darker))
> (define red (irgb 255 0 0))

a. What color do you expect (much-darker red) to compute? (Answer the question in terms of red, green, and blue values.)

b. Check your answer experimentally.

c. Set the top-left pixel of canvas to red.

d. What effect do you expect the following instruction to have?

> (image-transform-pixel! canvas 0 0 (compose irgb-lighter irgb-lighter))

e. Check your answer experimentally.

f. What effect do you expect the following instruction to have?

> (image-transform-pixel! canvas 0 0 
                          (compose irgb-lighter (compose irgb-lighter irgb-lighter)))

g. Check your answer experimentally.

Exercise 3: Composition and Order of Operations

Consider the composition (compose irgb-darker irgb-phaseshift).

a. Does this darken the image first or phase-shift the image first?

b. Does it matter? That is, do you get the same result either way?

c. Check your answer experimentally, using image-variant to compute the new images.

Exercise 4: Undoing Transformations

Earlier in this lab, we saw that some transformations had natural inverses and some did not. For example, if you complement a color twice, you get the original color. For many colors, if you lighten and then darken the color, you get the original color. (If any of the components is very large, then we may not be able to restore the original color.)

a. Consider the irgb-redder operation. How would you write an inverse to that operation using only the basic transformations along with composition? (That is, you do not get to write a lambda expression.)

b. Test your answer using image-variant. For example, if you decided that the answer was to make the image greener and bluer, you might write something like the following.

> (define redder-canvas (image-variant canvas irgb-redder))
> (define not-redder-canvas (image-variant redder-canvas (compose irgb-greener irgb-bluer)))
> (image-show redder-canvas)
> (image-show not-redder-canvas)

In case you were wondering, making the image greener and bluer does not invert the redder operation.

Exercise 5: Custom Transformations

a. Consider the following instructions. What effect do you expect them to have on the red component of each pixel? What effect do you expect it to have on the overall image?

> (image-show (image-variant picture 
                             (lambda (color)
                               (irgb (modulo (+ (irgb-red color) 128) 255) 
                                     (irgb-green color)
                                     (irgb-blue color)))))

b. Check your answers experimentally.

c. Write instructions to build a variant of picture in which the green component of each pixel is set to 255.

d. Write instructions to build a variant of picture in which the red component is restricted to the range [64..192], with numbers above 192 dropped to 192 and numbers below 64 raised to 64. In writing this, you may want to rely on the bound procedure you used in a previous lab.

e. Write instructions to build a variant of picture in which all the components are restricted to the range [64..192], with numbers above 192 dropped to 192 and numbers below 64 raised to 64. In writing this, you may want to rely on the bound procedure you used in a previous lab.

Explorations

Explorations are intended for students interested in further exploring the design aspects of these techniques. They also provide students who finish early with extra activities that may challenge them in different ways. You may do them in any order.

Exploration 1: Combining Transformations

While we only have a few basic transformations, there are, in some sense, an infinite number of ways to combine them. Try to find an interesting composition of basic transformations that someone might want to use as a filter.

Exploration 2: Arithmetic Transformations, Revised

As you have undoubtedly noticed, RGB colors are represented as integers. That means that we can transform colors with arithmetic operations as well as with component based operations. What do you think the following operations will do to your image? Try some of them to find out. Then, try a few of your own devising.

> (define t1 (lambda (c) (* 2 c)))
> (define t2 (lambda (c) (* 3 c)))
> (define t2 (lambda (c) (* -1 c)))
> (define t3 (lambda (c) (* 256 c)))
> (define t4 (lambda (c) (quotient (+ color-red c) 2)))
> (define t5 (lambda (c) (- c color-blue)))

Make sure you save your work regularly. Some of these procedures have the potential to crash the GIMP or MediaScript.

Extra 3: Your Own Transformations

Try developing your own interesting RGB transformation procedures and applying them to picture.