github.com/ccccaoqing/test@v0.0.0-20220510085219-3985d23445c0/doc/progs/image_draw.go (about) 1 // compile 2 3 // Copyright 2012 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 // This file contains the code snippets included in "The Go image/draw package." 8 9 package main 10 11 import ( 12 "image" 13 "image/color" 14 "image/draw" 15 ) 16 17 func main() { 18 Color() 19 Rect() 20 RectAndScroll() 21 ConvAndCircle() 22 Glyph() 23 } 24 25 func Color() { 26 c := color.RGBA{255, 0, 255, 255} 27 r := image.Rect(0, 0, 640, 480) 28 dst := image.NewRGBA(r) 29 30 // ZERO OMIT 31 // image.ZP is the zero point -- the origin. 32 draw.Draw(dst, r, &image.Uniform{c}, image.ZP, draw.Src) 33 // STOP OMIT 34 35 // BLUE OMIT 36 m := image.NewRGBA(image.Rect(0, 0, 640, 480)) 37 blue := color.RGBA{0, 0, 255, 255} 38 draw.Draw(m, m.Bounds(), &image.Uniform{blue}, image.ZP, draw.Src) 39 // STOP OMIT 40 41 // RESET OMIT 42 draw.Draw(m, m.Bounds(), image.Transparent, image.ZP, draw.Src) 43 // STOP OMIT 44 } 45 46 func Rect() { 47 dst := image.NewRGBA(image.Rect(0, 0, 640, 480)) 48 sr := image.Rect(0, 0, 200, 200) 49 src := image.Black 50 dp := image.Point{100, 100} 51 52 // RECT OMIT 53 r := image.Rectangle{dp, dp.Add(sr.Size())} 54 draw.Draw(dst, r, src, sr.Min, draw.Src) 55 // STOP OMIT 56 } 57 58 func RectAndScroll() { 59 dst := image.NewRGBA(image.Rect(0, 0, 640, 480)) 60 sr := image.Rect(0, 0, 200, 200) 61 src := image.Black 62 dp := image.Point{100, 100} 63 64 // RECT2 OMIT 65 r := sr.Sub(sr.Min).Add(dp) 66 draw.Draw(dst, r, src, sr.Min, draw.Src) 67 // STOP OMIT 68 69 m := dst 70 71 // SCROLL OMIT 72 b := m.Bounds() 73 p := image.Pt(0, 20) 74 // Note that even though the second argument is b, 75 // the effective rectangle is smaller due to clipping. 76 draw.Draw(m, b, m, b.Min.Add(p), draw.Src) 77 dirtyRect := b.Intersect(image.Rect(b.Min.X, b.Max.Y-20, b.Max.X, b.Max.Y)) 78 // STOP OMIT 79 80 _ = dirtyRect // noop 81 } 82 83 func ConvAndCircle() { 84 src := image.NewRGBA(image.Rect(0, 0, 640, 480)) 85 dst := image.NewRGBA(image.Rect(0, 0, 640, 480)) 86 87 // CONV OMIT 88 b := src.Bounds() 89 m := image.NewRGBA(b) 90 draw.Draw(m, b, src, b.Min, draw.Src) 91 // STOP OMIT 92 93 p := image.Point{100, 100} 94 r := 50 95 96 // CIRCLE2 OMIT 97 draw.DrawMask(dst, dst.Bounds(), src, image.ZP, &circle{p, r}, image.ZP, draw.Over) 98 // STOP OMIT 99 } 100 101 func theGlyphImageForAFont() image.Image { 102 return image.NewRGBA(image.Rect(0, 0, 640, 480)) 103 } 104 105 func theBoundsFor(index int) image.Rectangle { 106 return image.Rect(0, 0, 32, 32) 107 } 108 109 func Glyph() { 110 p := image.Point{100, 100} 111 dst := image.NewRGBA(image.Rect(0, 0, 640, 480)) 112 glyphIndex := 42 113 114 // GLYPH OMIT 115 src := &image.Uniform{color.RGBA{0, 0, 255, 255}} 116 mask := theGlyphImageForAFont() 117 mr := theBoundsFor(glyphIndex) 118 draw.DrawMask(dst, mr.Sub(mr.Min).Add(p), src, image.ZP, mask, mr.Min, draw.Over) 119 // STOP OMIT 120 } 121 122 //CIRCLESTRUCT OMIT 123 type circle struct { 124 p image.Point 125 r int 126 } 127 128 func (c *circle) ColorModel() color.Model { 129 return color.AlphaModel 130 } 131 132 func (c *circle) Bounds() image.Rectangle { 133 return image.Rect(c.p.X-c.r, c.p.Y-c.r, c.p.X+c.r, c.p.Y+c.r) 134 } 135 136 func (c *circle) At(x, y int) color.Color { 137 xx, yy, rr := float64(x-c.p.X)+0.5, float64(y-c.p.Y)+0.5, float64(c.r) 138 if xx*xx+yy*yy < rr*rr { 139 return color.Alpha{255} 140 } 141 return color.Alpha{0} 142 } 143 144 //STOP OMIT