Functional Problem Solving (CSC 151 2016S) : Labs
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Taking Notes] [Rubric]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Labs] [Outlines] [Readings] - [Examples] [Handouts]
Reference: [Setup] [Remote] [VM] [Errors] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Curtsinger (2016S)] [Davis (2013F)] [Rebelsky (2015F)] [Weinman (2014F)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)]
This lab was newly created for Spring 2016, and was only finished the evening before it was conducted. It is likely to have bugs.
Summary: In this laboratory, you will experiment with collage, or at least with copy and paste.
a. Bring up the Procedure Browser by selecting from the GIMP menu>.
b. Review the steps in copying and pasting. These steps follow.
(gimp-edit-copy-visible source),
where source is the number that
identifies the source image.
(gimp-edit-paste (image-get-layer
target) 1) where
target is the number that
identifies the source image.
> (define pasted (car (gimp-edit-paste (image-get-layer target) 1)))
(image-select-nothing! target)
to make sure nothing is currently selected.
gimp-layer-scale, gimp-item-transform-rotate,
and other related procedures.
(gimp-image-flatten target).
a. Open an existing image, such as our kitten image.
>(define source (image-show (image-load "/home/rebelsky/Desktop/kitten.jpg")))
b. Open a new 300x200 image for our collage.
>(define collage (image-show (image-new 300 200)))
c. Copy a 75x50 portion of the original image.
>(image-select-rectangle! source REPLACE ___ ___ 75 50)>(gimp-edit-copy-visible source)
d. Paste the copied portion to elsewhere in the collage.
>(image-select-rectangle! collage REPLACE 150 50 75 50)>(gimp-edit-paste (image-get-layer collage) 1)>(image-select-nothing! collage)>(gimp-image-flatten collage)
e. Paste the copied portion into four other locations in the collage. (You can choose the locations.)
As you likely noted, you did almost the exact same thing five times
in the previous code. That is, you selected a rectangle in the collage,
you called gimp-image-paste, you selected nothing, and
you flattened the result.
Write a procedure, (my-paste! ,
that encapsulates those steps.
image
left top
width height)
Check your code with the following.
(define kitten (image-show (image-load "/home/rebelsky/Desktop/kitten.jpg"))) (define collage (image-show (image-new 300 200))) (image-select-rectangle! kitten REPLACE 170 110 100 100) (gimp-edit-copy-visible kitten) (my-paste! collage 0 0 100 100) (my-paste! collage 100 0 100 100) (my-paste! collage 200 0 100 100) (my-paste! collage 0 100 100 100) (my-paste! collage 100 100 100 100)
a. Open an existing image, such as our kitten image.
>(define source (image-show (image-load "/home/rebelsky/Desktop/kitten.jpg")))
b. Open a new 300x200 image for our collage.
>(define collage (image-show (image-new 300 200)))
c. Copy a 75x50 portion of the original image.
>(image-select-rectangle! source REPLACE ___ ___ 75 50)>(gimp-edit-copy-visible source)
d. Paste the copied portion at a scale appropriate for the whole collage.
>(image-select-rectangle! collage REPLACE 0 0 300 200)>(define pasted (car (gimp-edit-paste (image-get-layer collage) 1))>(image-select-nothing! collage)>(gimp-layer-scale pasted 300 200 1)>(gimp-image-flatten collage)
Consider the following procedure.
(define p-and-s!
(lambda (image left top width height)
(image-select-rectangle! image REPLACE left top width height)
(let ([pasted (car (gimp-edit-paste (image-get-layer image) 1))])
(image-select-nothing! image)
(gimp-layer-scale pasted width height 1)
(gimp-image-flatten image)
image)))
a. What does this procdure do?
b. Why do we use a let in the middle of the procedure?
c. How could we achieve the same result without using let?
d. Use this procedure to add a few more scaled versions of your copied selection to our collage.
Consider the following procedure.
(define self!
(lambda (image)
(let ([width (image-width image)]
[height (image-height image)])
(image-select-rectangle! image REPLACE 0 0 width height)
(gimp-edit-copy-visible image)
(p-and-s! image
(* 1/10 width) (* 1/10 height)
(* 9/10 width) (* 8/10 height)))))
a. What does this procedure do?
b. What image do you expect to result from the following?
>(define kitten (image-show (image-load "/home/rebelsky/Desktop/kitten.jpg")))>(self! kitten)
c. Check your answer experimentally.
d. What do you expect to happen if we call self! again
on the same image?
>(self! kitten)
e. Check your answer experimentally.
Write a procedure, (my-paste-scale-and-rotate!
image angle left top width height) that pastes the copy
region into the selected area and then rotates by the given angle.
You will likely base this procedure on the p-and-s!
from an earlier problem.
Here's a typical call to rotate a pasted selection.
(gimp-item-transform-rotate pasted angle 1 0 0)
If you find that you have some extra time, you may play with making your own collages or you may attempt some of the following problems.
In the reading and in the rotation problems above, we used 1 0 0
as the last three paramaeters to gimp-item-transform-rotate.
By reading the documentation and by playing with different values, see
what other rotation effects you can achieve. (Hint: Use
0 x y with different values of x and
y.)
We've explored how to scale and rotate copied portions of an image.
But there are many other transformations available. Search for
gimp-item-transform in the Procedure Browser and see
what other interesting things we can do.
Write a procedure, (andy image), that creates a new
image that is the same size as image and contains four
identical (but smaller) copies of image in a 2x2 grid.
You can transform only part of an image by using the
image-select-rectangle! and image-transform!
procedures. For example,
>(define kitten (image-show (image-load "/home/rebelsky/Desktop/kitten.jpg")))>(define half-width (* 1/2 (image-width kitten)))>(define half-height (* 1/2 (image-height kitten)))>(define irgb-only-blue (section irgb-subtract <> (irgb 255 255 0)))>(image-select-rectangle! kitten REPLACE half-width 0 half-width half-height)>(image-transform! kitten irgb-only-blue)>(image-select-nothing! kitten)
Using a technique like this, make a more sophisticated drella
procedure that acts like andy, but recolors each of the
copies.