example_test.go 2.9 KB

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