?
本文檔使用 php中文網(wǎng)手冊(cè) 發(fā)布
import "image/draw"
Overview
Index
Examples
Package draw提供圖像合成功能。
有關(guān)此軟件包的介紹,請(qǐng)參閱“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 通過(guò)一個(gè)零掩碼調(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對(duì)齊,然后用Porter-Duff組合的結(jié)果替換dst中的矩形r。一個(gè)無(wú)面具被視為不透明。
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是一個(gè)抽屜,是Floyd-Steinberg誤差擴(kuò)散的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是一個(gè)image.Image,使用Set方法更改單個(gè)像素。
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通過(guò)調(diào)用Drawer接口操作來(lái)實(shí)現(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}