image_draw.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2012 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. // This file contains the code snippets included in "The Go image/draw package."
  5. package main
  6. import (
  7. "image"
  8. "image/color"
  9. "image/draw"
  10. )
  11. func main() {
  12. Color()
  13. Rect()
  14. RectAndScroll()
  15. ConvAndCircle()
  16. Glyph()
  17. }
  18. func Color() {
  19. c := color.RGBA{255, 0, 255, 255}
  20. r := image.Rect(0, 0, 640, 480)
  21. dst := image.NewRGBA(r)
  22. // ZERO OMIT
  23. // image.ZP is the zero point -- the origin.
  24. draw.Draw(dst, r, &image.Uniform{c}, image.ZP, draw.Src)
  25. // STOP OMIT
  26. // BLUE OMIT
  27. m := image.NewRGBA(image.Rect(0, 0, 640, 480))
  28. blue := color.RGBA{0, 0, 255, 255}
  29. draw.Draw(m, m.Bounds(), &image.Uniform{blue}, image.ZP, draw.Src)
  30. // STOP OMIT
  31. // RESET OMIT
  32. draw.Draw(m, m.Bounds(), image.Transparent, image.ZP, draw.Src)
  33. // STOP OMIT
  34. }
  35. func Rect() {
  36. dst := image.NewRGBA(image.Rect(0, 0, 640, 480))
  37. sr := image.Rect(0, 0, 200, 200)
  38. src := image.Black
  39. dp := image.Point{100, 100}
  40. // RECT OMIT
  41. r := image.Rectangle{dp, dp.Add(sr.Size())}
  42. draw.Draw(dst, r, src, sr.Min, draw.Src)
  43. // STOP OMIT
  44. }
  45. func RectAndScroll() {
  46. dst := image.NewRGBA(image.Rect(0, 0, 640, 480))
  47. sr := image.Rect(0, 0, 200, 200)
  48. src := image.Black
  49. dp := image.Point{100, 100}
  50. // RECT2 OMIT
  51. r := sr.Sub(sr.Min).Add(dp)
  52. draw.Draw(dst, r, src, sr.Min, draw.Src)
  53. // STOP OMIT
  54. m := dst
  55. // SCROLL OMIT
  56. b := m.Bounds()
  57. p := image.Pt(0, 20)
  58. // Note that even though the second argument is b,
  59. // the effective rectangle is smaller due to clipping.
  60. draw.Draw(m, b, m, b.Min.Add(p), draw.Src)
  61. dirtyRect := b.Intersect(image.Rect(b.Min.X, b.Max.Y-20, b.Max.X, b.Max.Y))
  62. // STOP OMIT
  63. _ = dirtyRect // noop
  64. }
  65. func ConvAndCircle() {
  66. src := image.NewRGBA(image.Rect(0, 0, 640, 480))
  67. dst := image.NewRGBA(image.Rect(0, 0, 640, 480))
  68. // CONV OMIT
  69. b := src.Bounds()
  70. m := image.NewRGBA(b)
  71. draw.Draw(m, b, src, b.Min, draw.Src)
  72. // STOP OMIT
  73. p := image.Point{100, 100}
  74. r := 50
  75. // CIRCLE2 OMIT
  76. draw.DrawMask(dst, dst.Bounds(), src, image.ZP, &circle{p, r}, image.ZP, draw.Over)
  77. // STOP OMIT
  78. }
  79. func theGlyphImageForAFont() image.Image {
  80. return image.NewRGBA(image.Rect(0, 0, 640, 480))
  81. }
  82. func theBoundsFor(index int) image.Rectangle {
  83. return image.Rect(0, 0, 32, 32)
  84. }
  85. func Glyph() {
  86. p := image.Point{100, 100}
  87. dst := image.NewRGBA(image.Rect(0, 0, 640, 480))
  88. glyphIndex := 42
  89. // GLYPH OMIT
  90. src := &image.Uniform{color.RGBA{0, 0, 255, 255}}
  91. mask := theGlyphImageForAFont()
  92. mr := theBoundsFor(glyphIndex)
  93. draw.DrawMask(dst, mr.Sub(mr.Min).Add(p), src, image.ZP, mask, mr.Min, draw.Over)
  94. // STOP OMIT
  95. }
  96. //CIRCLESTRUCT OMIT
  97. type circle struct {
  98. p image.Point
  99. r int
  100. }
  101. func (c *circle) ColorModel() color.Model {
  102. return color.AlphaModel
  103. }
  104. func (c *circle) Bounds() image.Rectangle {
  105. return image.Rect(c.p.X-c.r, c.p.Y-c.r, c.p.X+c.r, c.p.Y+c.r)
  106. }
  107. func (c *circle) At(x, y int) color.Color {
  108. xx, yy, rr := float64(x-c.p.X)+0.5, float64(y-c.p.Y)+0.5, float64(c.r)
  109. if xx*xx+yy*yy < rr*rr {
  110. return color.Alpha{255}
  111. }
  112. return color.Alpha{0}
  113. }
  114. //STOP OMIT