©
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
import "image/draw"
Overview
Index
Examples
Package draw提供图像合成功能。
有关此软件包的介绍,请参阅“Go图像/绘制软件包”:https://golang.org/doc/articles/image_draw.html
func Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point, op Op)
func DrawMask(dst Image, r image.Rectangle, src image.Image, sp image.Point, mask image.Image, mp image.Point, op Op)
type Drawer
type Image
type Op
func (op Op) Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point)
type Quantizer
Drawer (FloydSteinberg)
draw.go
func Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point, op Op)
Draw 通过一个零掩码调用 DrawMask
func DrawMask(dst Image, r image.Rectangle, src image.Image, sp image.Point, mask image.Image, mp image.Point, op Op)
DrawMask将dst中的r.Min与src中的sp和mask中的mp对齐,然后用Porter-Duff组合的结果替换dst中的矩形r。一个无面具被视为不透明。
Drawer 包含Draw方法。
type Drawer interface { // Draw aligns r.Min in dst with sp in src and then replaces the // rectangle r in dst with the result of drawing src on dst. Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point)}
FloydSteinberg是一个抽屉,是Floyd-Steinberg误差扩散的Src操作。
var FloydSteinberg Drawer = floydSteinberg{}
package mainimport ("fmt""image""image/color""image/draw""math")func main() {const width = 130const height = 50 im := image.NewGray(image.Rectangle{Max: image.Point{X: width, Y: height}})for x := 0; x < width; x++ {for y := 0; y < height; y++ { dist := math.Sqrt(math.Pow(float64(x-width/2), 2)/3+math.Pow(float64(y-height/2), 2)) / (height / 1.5) * 255var gray uint8if dist > 255 { gray = 255} else { gray = uint8(dist)} im.SetGray(x, y, color.Gray{Y: 255 - gray})}} pi := image.NewPaletted(im.Bounds(), []color.Color{ color.Gray{Y: 255}, color.Gray{Y: 160}, color.Gray{Y: 70}, color.Gray{Y: 35}, color.Gray{Y: 0},}) draw.FloydSteinberg.Draw(pi, im.Bounds(), im, image.ZP) shade := []string{" ", "░", "▒", "▓", "█"}for i, p := range pi.Pix { fmt.Print(shade[p])if (i+1)%width == 0 { fmt.Print("\n")}}}
Image是一个image.Image,使用Set方法更改单个像素。
type Image interface { image.Image Set(x, y int, c color.Color)}
Op是Porter-Duff合成操作员。
type Op int
const ( // Over specifies ``(src in mask) over dst''. Over Op = iota // Src specifies ``src in mask''. Src)
func (op Op) Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point)
Draw通过调用Drawer接口操作来实现抽屉界面。
Quantizer为图像生成调色板。
type Quantizer interface { // Quantize appends up to cap(p) - len(p) colors to p and returns the // updated palette suitable for converting m to a paletted image. Quantize(p color.Palette, m image.Image) color.Palette}