Version: 5.1
Picturing Programs Teachpack
1 About This Teachpack
Provides a variety of functions for combining and manipulating images
and running interactive animations.
It’s intended to be used with the textbook
Picturing Programs.
2 Installation
This package should be bundled with DrRacket version 5.1 and later, so there should be
no installation procedure.
3 Functions from image.rkt and universe.rkt
This package includes all of
the image teachpack and
and
the universe teachpack,
so if you’re using this teachpack, don’t also load either of those.
See the above links for how to use those teachpacks.
It also supersedes the older tiles and sb-world teachpacks,
so if you have those, don’t load them either; use this instead.
This package also provides the following additional functions:
4 New image functions
Rotates an image 90 degrees clockwise.
Rotates an image 90 degrees counterclockwise.
Rotates an image 180 degrees around its center.
Chops off the specified number of pixels from the top of the image.
Chops off the specified number of pixels from the bottom of the image.
Chops off the specified number of pixels from the left side of the image.
Chops off the specified number of pixels from the right side of the image.
Returns the given image unaltered. Useful as a draw handler for animations whose model is an image.
5 Variables
This teachpack also defines variable names for some of the pictures used in the textbook.
A picture of the author, c. 2005.
A picture of a stone tablet with
hieroglyphics on it.
A picture of a student sitting at a
computer.
A picture of a book with a question mark.
A picture of a stick figure, built
from geometric primitives.
A picture of a DrScheme/DrRacket
logo.
A picture of an appointment calendar.
Note that these seven variable names happen to start with "pic:", to
distinguish them from anything you might define that happens to be named
"calendar" or "book", but you can name a variable anything you want; in
particular, there’s no requirement that your names start with "pic:".
6 Pixel functions
The above functions allow you to operate on a picture as a whole, but sometimes
you want to manipulate a picture pixel-by-pixel.
6.1 Colors and pixels
Each pixel of a bitmap image has a color, a built-in structure with
four components – red, green, blue, and alpha – each represented by an
integer from 0 to 255. Larger alpha values are "more opaque": an image with
alpha=255 is completely opaque, and one with alpha=0 is completely
transparent.
Even if you’re not trying to get transparency effects, alpha is also used
for dithering to smooth out jagged edges. In
(circle 50 "solid" "red"), the pixels inside the circle are pure
red, with alpha=255; the pixels outside the circle are transparent (alpha=0);
and the pixels on the boundary are red with various alpha values (for example,
if one quarter of a pixel’s area is inside
the mathematical boundary of the circle, that pixel’s alpha value will be
63).
Given a color name like "red", ’turquoise, "forest green",
etc., returns the corresponding
color struct, showing the red, green, blue, and alpha components. If the name isn’t
recognized, returns
false.
Similar to
name->color, but accepts colors and
false as
well: colors produce themselves, while
false produces a transparent
color.
Compares two colors for equality. As with
colorize, treats
false as a transparent color (i.e. with an alpha-component of 0).
All colors with alpha=0 are considered equal to one another, even if they have
different red, green, or blue components.
Gets the color of a specified pixel in the given image. If x and/or y are outside
the bounds of the image, returns a transparent color.
6.2 Specifying the color of each pixel of an image
Builds an image of the specified size and shape by calling the specified function
on the coordinates of each pixel. For example,
produces a fuzzy version of the given picture by replacing each pixel with a
randomly chosen pixel near it.
A version of
build-image for students who don’t know about structs yet.
Each of the four functions takes in the x and y coordinates of a pixel, and
should return an integer from 0 through 255 to determine that color component.
Just like
build4-image, but without specifying the alpha component
(which defaults to 255, fully opaque).
Applies the given function to each pixel in a given image, producing a new image the same
size and shape. For example,
produces a copy of my-picture with all the red leached out,
leaving only the blue and green components.
Since
make-color with three arguments defaults alpha to 255,
this definition of
lose-red discards any alpha information (including edge-dithering)
that was in the original image. To preserve this information, one could write
Another example:
produces a picture the size of my-picture’s bounding rectangle,
with a smooth color gradient with red increasing from left to
right and blue increasing from top to bottom.
A version of map-image for students who don’t know about structs yet. Each of the
four given functions is assumed to have the contract
num(x) num(y) num(r) num(g) num(b) num(alpha) -> num For each pixel in the original picture, applies the four
functions to the x coordinate, y coordinate, red, green, blue, and alpha
components of the pixel.
The results of the four functions are used as the red, green, blue, and alpha
components in the corresponding pixel of the resulting picture.
For example,
(define (zero x y r g b a) 0) |
(define (same-g x y r g b a) g) |
(define (same-b x y r g b a) b) |
(define (same-alpha x y r g b a) a) |
(map4-image zero same-g same-b same-alpha my-picture) |
produces a copy of my-picture with all the red leached out,
leaving only the blue, green, and alpha components.
produces an opaque picture the size of my-picture’s bounding rectangle,
with a smooth color gradient with red increasing from left to
right and blue increasing from top to bottom.
Like
map4-image, but not specifying the alpha component. Note that
the red, green, and blue functions also
don’t take in alpha values.
Each of the three given functions is assumed to have the contract
num(x) num(y) num(r) num(g) num(b) -> num For each pixel in the original picture, applies the three functions to the x
coordinate, y coordinate, red, green, and blue components of the pixel.
The results are used as a the red, green, and blue components in the
corresponding pixel of the resulting picture.
The alpha component in the resulting picture is copied from the source
picture. For example,
produces a copy of my-picture with all the red leached out; parts of
the picture that were transparent are still transparent, and parts that were
dithered are still dithered.
produces a my-picture-shaped "window" on a color-gradient.
Not specific to colors, but useful if you’re building colors by arithmetic.
For example,
The version using bad-gradient crashes because color components must be exact integers.
The version using good-gradient works.
7 Input and Output
This teachpack also provides several functions to help in testing
I/O functions (in Advanced Student language; ignore this section if
you’re in a Beginner or Intermediate language):
Calls
thunk, which presumably uses
read,
in such a way that
read reads from
input rather than from
the keyboard.
Calls
thunk, which presumably uses
display,
print,
write, and/or
printf, in such a way that its output is
accumlated into a string, which is then returned.
Calls
thunk, which presumably uses
read,
in such a way that
read reads from the specified file
rather than from the keyboard.
Calls
thunk, which presumably uses
display,
print,
write, and/or
printf, in such a way that its output is
redirected into the specified file.
Calls
thunk, which presumably uses
read,
in such a way that
read reads from the HTML source of the
Web page at the specified URL rather than from the keyboard.