?
This document uses PHP Chinese website manual Release
import "image/draw"
Overview
Index
Examples
Package draw提供圖像合成功能。
有關(guān)此軟件包的介紹,請參閱“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 通過一個零掩碼調(diào)用 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組合的結(jié)果替換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通過調(diào)用Drawer接口操作來實現(xiàn)抽屜界面。
Quantizer為圖像生成調(diào)色板。
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}