Fundamentals of CS I (CS151 2002F)

Homework 7: Mapping Images

Assigned: Friday, November 1, 2002
Due: Wednesday, November 6, 2002
No extensions!

Summary: In this assignment, you will work with and build procedures that apply other procedures to the pixels in an image.

Purposes: To give you more experience working with higher-order procedures.

Collaboration: You must work in assigned groups:

Helpful References:

Contents:

Background

You should be familiar with the map procedure that applies a function to each value in a list. Here is a similar procedure that applies a function to each pixel in an image, putting the result into a new image.

;;; Procedure:
;;;   gsfu-map-image
;;; Parameters:
;;;   image, the id of a valid image.
;;;   layer, the id of a layer on that image.
;;;   func, the function to be applied to 
;;; Purpose:
;;;   Creates a new image by applying func to each pixel in layer.
;;; Produces:
;;;   (new-image new-layer), a new image and its layer
;;; Preconditions:
;;;   func must take a color (a list of three 0-255 values) as a
;;;   parameter and return another color.
;;; Postconditions:
;;;   new-layer on new-image contains the result of applying func 
;;;   to every pixel in layer in image.
;;; Implementation Note:
;;;   Because script-fu does not do well with excessive recursion,
;;;   there are two recursive helper procedures, one that recurses
;;;   through a single row (map-row) and one that recurses through
;;;   all the rows (map-kernel).  
(define gsfu-map-image
  (lambda (image layer func)
    (let* (
           ; Compute the width and height
           (width (car (gimp-image-width image)))
           (height (car (gimp-image-height image)))
           ; Build a new image and layer
           (new-image-and-layer (gsfu-new-image width height))
           (new-image (car new-image-and-layer))
           (new-layer (cadr new-image-and-layer))
          )
      (letrec (
               ; (map-row x y)
               ;   Compute the pixels in row y, starting with column x.
               (map-row
                 (lambda (x y)
                   (if (< x width)
                     (begin
                       ; Compute the new pixel at (x,y)
                       (gsfu-set-color new-layer x y
                                       (func (gsfu-get-color layer x y)))
                       ; Continue with the rest of the row.
                       (map-row (+ x 1) y)))))
               ; (map-kernel y)
               ;   Compute the pixels in rows y and beyond
               (map-kernel
                 (lambda (y)
                   (if (< y height)
                       (begin
                         (map-row 0 y func)
                         (map-kernel (+ 1 y)))
                       (list new-image new-layer))))
              )
        (map-kernel 0)))))

You've experimented with this procedure in the lab on algorithmic art.

Assignment: Merging Images

1. Write a procedure, (merge-images image1 layer1 image2 layer2 mergefunc), that merges two images into a new image by applying mergefunc to each pair of pixels.

2. Write and try each of the following merge functions.

3. Design three of your own interesting merge functions.

4. Summarize your observations on the success and failure of each function (both those from problem 2 and those from problem 3).

5. Pick a particularly interesting result of merging two images and send me a link to it.

 

History

Tuesday, 29 October 2002 [Samuel A. Rebelsky]

Friday, 1 November 2002 [Samuel A. Rebelsky]

 

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 Wed Dec 4 08:45:38 2002.
The source to the document was last modified on Fri Nov 1 08:13:34 2002.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Homework/hw.07.html.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu