1
2
3
4
5 package draw_test
6
7 import (
8 "fmt"
9 "image"
10 "image/color"
11 "image/png"
12 "log"
13 "math"
14 "os"
15
16 "golang.org/x/image/draw"
17 "golang.org/x/image/math/f64"
18 )
19
20 func ExampleDraw() {
21 fSrc, err := os.Open("../testdata/blue-purple-pink.png")
22 if err != nil {
23 log.Fatal(err)
24 }
25 defer fSrc.Close()
26 src, err := png.Decode(fSrc)
27 if err != nil {
28 log.Fatal(err)
29 }
30
31 dst := image.NewRGBA(image.Rect(0, 0, 400, 300))
32 green := image.NewUniform(color.RGBA{0x00, 0x1f, 0x00, 0xff})
33 draw.Copy(dst, image.Point{}, green, dst.Bounds(), draw.Src, nil)
34 qs := []draw.Interpolator{
35 draw.NearestNeighbor,
36 draw.ApproxBiLinear,
37 draw.CatmullRom,
38 }
39 const cos60, sin60 = 0.5, 0.866025404
40 t := f64.Aff3{
41 +2 * cos60, -2 * sin60, 100,
42 +2 * sin60, +2 * cos60, 100,
43 }
44
45 draw.Copy(dst, image.Point{20, 30}, src, src.Bounds(), draw.Over, nil)
46 for i, q := range qs {
47 q.Scale(dst, image.Rect(200+10*i, 100*i, 600+10*i, 150+100*i), src, src.Bounds(), draw.Over, nil)
48 }
49 draw.NearestNeighbor.Transform(dst, t, src, src.Bounds(), draw.Over, nil)
50
51 red := image.NewNRGBA(image.Rect(0, 0, 16, 16))
52 for y := 0; y < 16; y++ {
53 for x := 0; x < 16; x++ {
54 red.SetNRGBA(x, y, color.NRGBA{
55 R: uint8(x * 0x11),
56 A: uint8(y * 0x11),
57 })
58 }
59 }
60 red.SetNRGBA(0, 0, color.NRGBA{0xff, 0xff, 0x00, 0xff})
61 red.SetNRGBA(15, 15, color.NRGBA{0xff, 0xff, 0x00, 0xff})
62
63 ops := []draw.Op{
64 draw.Over,
65 draw.Src,
66 }
67 for i, op := range ops {
68 dr := image.Rect(120+10*i, 150+60*i, 170+10*i, 200+60*i)
69 draw.NearestNeighbor.Scale(dst, dr, red, red.Bounds(), op, nil)
70 t := f64.Aff3{
71 +cos60, -sin60, float64(190 + 10*i),
72 +sin60, +cos60, float64(140 + 50*i),
73 }
74 draw.NearestNeighbor.Transform(dst, t, red, red.Bounds(), op, nil)
75 }
76
77 dr := image.Rect(0, 0, 128, 128)
78 checkerboard := image.NewAlpha(dr)
79 for y := dr.Min.Y; y < dr.Max.Y; y++ {
80 for x := dr.Min.X; x < dr.Max.X; x++ {
81 if (x/20)%2 == (y/20)%2 {
82 checkerboard.SetAlpha(x, y, color.Alpha{0xff})
83 }
84 }
85 }
86 sr := image.Rect(0, 0, 16, 16)
87 circle := image.NewAlpha(sr)
88 for y := sr.Min.Y; y < sr.Max.Y; y++ {
89 for x := sr.Min.X; x < sr.Max.X; x++ {
90 dx, dy := x-10, y-8
91 if d := 32 * math.Sqrt(float64(dx*dx)+float64(dy*dy)); d < 0xff {
92 circle.SetAlpha(x, y, color.Alpha{0xff - uint8(d)})
93 }
94 }
95 }
96 cyan := image.NewUniform(color.RGBA{0x00, 0xff, 0xff, 0xff})
97 draw.NearestNeighbor.Scale(dst, dr, cyan, sr, draw.Over, &draw.Options{
98 DstMask: checkerboard,
99 SrcMask: circle,
100 })
101
102
103 if false {
104 fDst, err := os.Create("out.png")
105 if err != nil {
106 log.Fatal(err)
107 }
108 defer fDst.Close()
109 err = png.Encode(fDst, dst)
110 if err != nil {
111 log.Fatal(err)
112 }
113 }
114
115 fmt.Printf("dst has bounds %v.\n", dst.Bounds())
116
117
118 }
119
View as plain text