1.1 Manipulating Images: "image.rkt"
(require htdp/image) |
NOTE: This library is deprecated. Use 2htdp/image instead. For the foreseeable time, we will continue to support the teachpack for your existing programs.
The teachpack provides functions for constructing and manipulating images. Basic, colored images are created as outlines or solid shapes. Additional functions allow for the composition of images.
1.1.1 Images
1.1.2 Modes and Colors
Mode (one-of/c 'solid 'outline "solid" "outline")
A Mode is used to specify whether painting a shape fills or outlines the form.
struct
(struct color (red green blue) #:extra-constructor-name make-color) red : (and/c natural-number/c (<=/c 255)) green : (and/c natural-number/c (<=/c 255)) blue : (and/c natural-number/c (<=/c 255))
RGB color?
A RGB describes a color via a shade of red, blue, and green colors (e.g., (make-color 100 200 30)).
Color (or/c symbol? string? color?)
A Color is a color-symbol (e.g., 'blue) or a color-string (e.g., "blue") or an RGB structure.
procedure
(image-color? x) → boolean?
x : any
1.1.3 Creating Basic Shapes
In DrRacket, you can insert images from your file system. Use PNG images whenever possible. In addition, you can create basic shapes with the following functions.
procedure
w : (and/c number? (or/c zero? positive?)) h : (and/c number? (or/c zero? positive?)) m : Mode c : Color
procedure
w : (and/c number? (or/c zero? positive?)) h : (and/c number? (or/c zero? positive?)) m : Mode c : Color
procedure
n : (and/c number? (>=/c 2)) outer : (and/c number? (>=/c 1)) inner : (and/c number? (>=/c 1)) m : Mode c : Color
procedure
(regular-polygon s r m c [angle]) → image?
s : side r : number? m : Mode c : Color angle : real? = 0
1.1.4 Basic Image Properties
To understand how images are manipulated, you need to understand the basic properties of images.
procedure
(image-width i) → integer?
i : image?
procedure
(image-height i) → integer?
i : image?
For the composition of images, you must know about pinholes. Every image come with a pinhole. For images created with the above functions, the pinhole is at the center of the shape except for those created from line and text. The text function puts the pinhole at the upper left corner of the image, and line puts the pinhole at the beginning of the line (meaning that if the first two arguments to line are positive, the pinhole is also in the upper left corner). The pinhole can be moved, of course, and compositions locate pinholes according to their own rules. When in doubt you can always find out where the pinhole is and place it where convenient.
procedure
(put-pinhole i x y) → image?
i : image? x : number? y : number?
procedure
(move-pinhole i delta-x delta-y) → image?
i : image? delta-x : number? delta-y : number?
1.1.5 Composing Images
Images can be composed, and images can be found within compositions.
procedure
(overlay/xy img delta-x delta-y other) → image?
img : image? delta-x : number? delta-y : number? other : image?
The pinhole of the resulting image is the same place as the pinhole in the first image.
(overlay img (move-pinhole other (- delta-x) (- delta-y)))
procedure
(image-inside? img other) → boolean?
img : image? other : image?
Be careful when using this function with jpeg images. If you use an image-editing program to crop a jpeg image and then save it, image-inside? does not recognize the cropped image, due to standard compression applied to JPEG images.
procedure
(find-image img other) → posn?
img : image? other : image?
1.1.6 Manipulating Images
Images can also be shrunk. These “shrink” functions trim an image by eliminating extraneous pixels.
1.1.7 Scenes
A scene is an image, but with the pinhole in the upper-left corner, i.e. an image where pinhole-x and pinhole-y both return 0.
Scenes are particularly useful with the 2htdp/universe and htdp/world teachpacks, since it displays only scenes in its canvas.
procedure
(empty-scene width height) → scene?
width : natural-number/c height : natural-number/c
procedure
(nw:rectangle width height solid-or-outline c) → image? width : natural-number/c height : natural-number/c solid-or-outline : Mode c : Color
procedure
(scene+line s x0 y0 x1 y1 c) → scene?
s : scene? x0 : number? y0 : number? x1 : number? y1 : number? c : Color
1.1.8 Miscellaneous Image Manipulation and Creation
The last group of functions extracts the constituent colors from an image and converts a list of colors into an image.
value
List-of-color : list?
procedure
(image->color-list img) → List-of-color
img : image?
procedure
(color-list->image l width height x y) → image?
l : List-of-color width : natural-number/c height : natural-number/c x : natural-number/c y : natural-number/c
The remaining functions provide alpha-channel information as well. Alpha channels are a measure of transparency; 0 indicates fully opaque and 255 indicates fully transparent.
struct
(struct alpha-color (alpha red green blue) #:extra-constructor-name make-alpha-color) alpha : (and/c natural-number/c (<=/c 255)) red : (and/c natural-number/c (<=/c 255)) green : (and/c natural-number/c (<=/c 255)) blue : (and/c natural-number/c (<=/c 255))
procedure
(image->alpha-color-list img) → (list-of alpha-color?)
img : image?
procedure
(alpha-color-list->image l width height x y) → image?
l : (list-of alpha-color?) width : integer? height : integer? x : integer? y : integer?