在本页中:
1.1.1 图像
image?
image=?
1.1.2 模式和颜色
color
image-color?
1.1.3 创建基本图形
rectangle
circle
ellipse
triangle
star
regular-polygon
line
text
1.1.4 基本图像属性
image-width
image-height
pinhole-x
pinhole-y
put-pinhole
move-pinhole
1.1.5 图像的组合
add-line
overlay
overlay/  xy
image-inside?
find-image
1.1.6 图像的操作
shrink-tl
shrink-tr
shrink-bl
shrink-br
shrink
1.1.7 场景
scene?
empty-scene
place-image
nw:  rectangle
scene+  line
1.1.8 其他图像处理和创建函数
List-of-color
image->color-list
color-list->image
alpha-color
image->alpha-color-list
alpha-color-list->image

1.1 操作图像: "image.rkt"

 (require htdp/image) package: htdp-lib

注意:该库已被废弃;请改用2htdp/image。在可预见的将来,我们将继续支持现有程序中的教学包。

本教学包提供创建和操作图像的函数。创建的基本彩色图像为空心或实心形状。其他函数允许组合图像。

1.1.1 图像

函数

(image? x)  boolean?

  x : any/c
x是图像吗?

函数

(image=? x y)  boolean?

  x : image?
  y : image?
xy是相同的图像吗?

1.1.2 模式和颜色

Mode (one-of/c 'solid 'outline "solid" "outline")

Mode(模式)用来指定绘制图形时是将其填充满颜色还是只绘制边框。

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?

RGB通过红色、绿色和蓝色的色调描述颜色(例如,(make-color 100 200 30))。

Color (or/c symbol? string? color?)

Color是颜色符号(例如'blue)、颜色字符串(例如"blue")或RGB结构体。

函数

(image-color? x)  boolean?

  x : any
判断输入是否是有效的图像Color

1.1.3 创建基本图形

DrRacket可以插入来自文件系统的图像。请尽可能使用PNG格式。也可以使用以下函数创建基本图形。

函数

(rectangle w h m c)  image?

  w : (and/c number? (or/c zero? positive?))
  h : (and/c number? (or/c zero? positive?))
  m : Mode
  c : Color
创建wh的矩形,按m填充,并使用颜色c绘制。

函数

(circle r m c)  image?

  r : (and/c number? (or/c zero? positive?))
  m : Mode
  c : Color
创建半径为r的圆盘,按m填充,并使用颜色c绘制。

函数

(ellipse w h m c)  image?

  w : (and/c number? (or/c zero? positive?))
  h : (and/c number? (or/c zero? positive?))
  m : Mode
  c : Color
创建wh的椭圆,按m填充,并使用颜色c绘制。

函数

(triangle s m c)  image?

  s : number?
  m : Mode
  c : Color
创建指向向上、边长为s像素的等边三角形,按m填充,并使用颜色c绘制。

函数

(star n outer inner m c)  image?

  n : (and/c number? (>=/c 2))
  outer : (and/c number? (>=/c 1))
  inner : (and/c number? (>=/c 1))
  m : Mode
  c : Color
创建n角星,其中点到中心的最大距离为outer半径,点到中心的最小距离为inner半径。

函数

(regular-polygon s r m c [angle])  image?

  s : side
  r : number?
  m : Mode
  c : Color
  angle : real? = 0
使用模式m和颜色c创建多边形,其边数为s,内接于半径为r的圆。如果指定了angle(角度),则将多边形旋转这个角度。

函数

(line x y c)  image?

  x : number?
  y : number?
  c : Color
创建颜色为c、从(0,0)到(x ,y)的线。参见下面的add-line

函数

(text s f c)  Image

  s : string?
  f : (and/c number? positive?)
  c : Color
创建字体大小f、颜色c、文本s的图像。

1.1.4 基本图像属性

要了解如何操作图像,先需要了解图像的基本属性。

函数

(image-width i)  integer?

  i : image?
获取i的宽度(单位为像素)

函数

(image-height i)  integer?

  i : image?
获取i的高度(单位为像素)

对于图像的组合,必须了解pinhole(针孔)的概念。每张图片都带有pinhole。 对于使用前述函数创建的图像,除了linetext之外,pinhole都位于形状的中心。 text函数将pinhole放在图像的左上角, 而line将pinhole放在线的开头(也就是说,如果line的前两个参数线为正数,那么pinhole位于左上角)。 当然,pinhole可以移动,并且图像组合物根据自己的规则定位pinhole。 不确定的话,可以随时找到pinhole的位置,或者根据需要放置pinhole。

函数

(pinhole-x i)  integer?

  i : image?
求pinhole的x坐标,从图像左侧开始计算。

函数

(pinhole-y i)  integer?

  i : image?
求pinhole的y坐标,从图像顶部(向下)计算。

函数

(put-pinhole i x y)  image?

  i : image?
  x : number?
  y : number?
创建pinhole位于xy的新图像,分别从图像左侧和顶部(向下)开始计算。

函数

(move-pinhole i delta-x delta-y)  image?

  i : image?
  delta-x : number?
  delta-y : number?
创建新图像,将pinhole从当前位置向右和向下移动delta-xdelta-y像素。 要向左或向上移动的话,使用负数。

1.1.5 图像的组合

可以组合图像,也可以在组合中寻找图像。

函数

(add-line i x1 y1 x2 y2 c)  image?

  i : image?
  x1 : number?
  y1 : number?
  x2 : number?
  y2 : number?
  c : Color
创建新图像,将从(x1,y1)到(x2,y2)的线加入图像i

函数

(overlay img img2 img* ...)  image?

  img : image?
  img2 : image?
  img* : image?
将所有图像按pinhole位置叠加,创建新图像。新图像的pinhole与第一个图像的pinhole相同。

函数

(overlay/xy img delta-x delta-y other)  image?

  img : image?
  delta-x : number?
  delta-y : number?
  other : image?
other的像素加到img中,创建新图像。

不将两个图像按pinhole叠加,而是将other的pinhole放置于点:
(make-posn (+ (pinhole-x img) delta-x)
           (+ (pinhole-y img) delta-y))

新图像的pinhole与第一个图像的pinhole相同。

组合move-pinholeoverlay也可以产生相同的效果,
(overlay img
         (move-pinhole other
                       (- delta-x)
                       (- delta-y)))

函数

(image-inside? img other)  boolean?

  img : image?
  other : image?
判断第二个图像的像素是否出现在第一个图像中。

将此函数与jpeg图像一起使用时要小心。如果使用图像编辑程序裁剪jpeg图像然后保存之, 由于JPEG图像的压缩,因此image-inside?无法识别裁剪后的图像。

函数

(find-image img other)  posn?

  img : image?
  other : image?
求第二个图像的像素出现在第一个图像中的(相对于第一个图像的pinhole的)位置。如果(image-inside? img other)不成立,find-image抛出错误。

1.1.6 图像的操作

图像也可以被缩小。这些“缩小”函数消除不再需要的像素,从而减小图像。

函数

(shrink-tl img width height)  image?

  img : image?
  width : number?
  height : number?
左上角开始,将图像缩小到widthheight大。所得图像的pinhole位于其中心。

函数

(shrink-tr img width height)  image?

  img : image?
  width : number?
  height : number?
右上角开始,将图像缩小到widthheight大。所得图像的pinhole位于其中心。

函数

(shrink-bl img width height)  image?

  img : image?
  width : number?
  height : number?
左下角开始,将图像缩小到widthheight大。所得图像的pinhole位于其中心。

函数

(shrink-br img width height)  image?

  img : image?
  width : number?
  height : number?
右下角开始,将图像缩小到widthheight大。所得图像的pinhole位于其中心。

函数

(shrink img left above right below)  image?

  img : image?
  left : number?
  above : number?
  right : number?
  below : number?
围绕pinhole收缩图像。数值参数分别是保存位于针孔左侧、上方、右侧和下方的像素数。pinhole所对应的像素总会被保存。

1.1.7 场景

scene(场景)是pinhole位于其左上角的图像,即pinhole-xpinhole-y都返回0

2htdp/universehtdp/world教学包来说, 场景特别有用,因为它们在画布中只能显示scene

函数

(scene? x)  boolean?

  x : any/c
x是场景吗?

函数

(empty-scene width height)  scene?

  width : natural-number/c
  height : natural-number/c
创建纯白色、widthheightscene

函数

(place-image img x y s)  scene?

  img : image?
  x : number?
  y : number?
  s : scene?
img放置到s(x, y)位置,创建场景; (x, y)是计算机图形坐标,也就是说它们从左上角向右和向下计数。

函数

(nw:rectangle width    
  height    
  solid-or-outline    
  c)  image?
  width : natural-number/c
  height : natural-number/c
  solid-or-outline : Mode
  c : Color
创建widthheight的矩形,由solid-or-outline决定实心还是边框,由c决定颜色,其中pinhole位于左上角。

函数

(scene+line s x0 y0 x1 y1 c)  scene?

  s : scene?
  x0 : number?
  y0 : number?
  x1 : number?
  y1 : number?
  c : Color
绘制颜色为c、从计算机图形坐标(x0, y0)(x1, y1)的线,从而创建场景。和add-line函数不同,scene+line会切除线超出s边界的部分。

1.1.8 其他图像处理和创建函数

最后这组函数从图像中提取成分颜色,并将颜色表转换为图像。

List-of-color : list?

是以下之一:
;  empty
;  (cons Color List-of-color)
; 解释:表示颜色的表。

函数

(image->color-list img)  List-of-color

  img : image?
将图像转换为颜色的表。

函数

(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
将颜色表l转换为图像,其宽度为width, 高度为height,pinhole坐标相对图像左上角为(x,y)。

后续的函数也提供alpha通道信息。Alpha通道是衡量透明度的标准;0表示完全不透明,255表示完全透明。

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))
表示alpha颜色的结构体。

函数

(image->alpha-color-list img)  (list-of alpha-color?)

  img : image?
将图像转换为alpha颜色的表

函数

(alpha-color-list->image l width height x y)  image?

  l : (list-of alpha-color?)
  width : integer?
  height : integer?
  x : integer?
  y : integer?
alpha-colorl转换为图像,其宽度为width, 高度为height,pinhole坐标相对图像左上角为(x,y)。