image.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package captcha
  2. import (
  3. "image"
  4. "image/png"
  5. "io"
  6. "os"
  7. "rand"
  8. "time"
  9. )
  10. const (
  11. // Standard width and height of a captcha image.
  12. StdWidth = 300
  13. StdHeight = 80
  14. maxSkew = 2
  15. )
  16. type Image struct {
  17. *image.NRGBA
  18. primaryColor image.NRGBAColor
  19. numWidth int
  20. numHeight int
  21. dotSize int
  22. }
  23. func init() {
  24. rand.Seed(time.Seconds())
  25. }
  26. // NewImage returns a new captcha image of the given width and height with the
  27. // given digits, where each digit must be in range 0-9.
  28. func NewImage(digits []byte, width, height int) *Image {
  29. img := new(Image)
  30. img.NRGBA = image.NewNRGBA(width, height)
  31. img.primaryColor = image.NRGBAColor{
  32. uint8(rand.Intn(129)),
  33. uint8(rand.Intn(129)),
  34. uint8(rand.Intn(129)),
  35. 0xFF,
  36. }
  37. // Calculate sizes.
  38. img.calculateSizes(width, height, len(digits))
  39. // Draw background (10 random circles of random brightness).
  40. img.fillWithCircles(10, img.dotSize)
  41. // Randomly position captcha inside the image.
  42. maxx := width - (img.numWidth+img.dotSize)*len(digits) - img.dotSize
  43. maxy := height - img.numHeight - img.dotSize*2
  44. x := rnd(img.dotSize*2, maxx)
  45. y := rnd(img.dotSize*2, maxy)
  46. // Draw digits.
  47. for _, n := range digits {
  48. img.drawDigit(font[n], x, y)
  49. x += img.numWidth + img.dotSize
  50. }
  51. // Draw strike-through line.
  52. img.strikeThrough()
  53. return img
  54. }
  55. // WriteTo writes captcha image in PNG format into the given writer.
  56. //
  57. // Bug: while Image conforms to io.WriterTo interface, this function returns 0
  58. // instead of the actual bytes written because png.Encode doesn't report this.
  59. func (img *Image) WriteTo(w io.Writer) (int64, os.Error) {
  60. return 0, png.Encode(w, img)
  61. }
  62. func (img *Image) calculateSizes(width, height, ncount int) {
  63. // Goal: fit all digits inside the image.
  64. var border int
  65. if width > height {
  66. border = height / 5
  67. } else {
  68. border = width / 5
  69. }
  70. // Convert everything to floats for calculations.
  71. w := float64(width - border*2)
  72. h := float64(height - border*2)
  73. // fw takes into account 1-dot spacing between digits.
  74. fw := float64(fontWidth) + 1
  75. fh := float64(fontHeight)
  76. nc := float64(ncount)
  77. // Calculate the width of a single digit taking into account only the
  78. // width of the image.
  79. nw := w / nc
  80. // Calculate the height of a digit from this width.
  81. nh := nw * fh / fw
  82. // Digit too high?
  83. if nh > h {
  84. // Fit digits based on height.
  85. nh = h
  86. nw = fw / fh * nh
  87. }
  88. // Calculate dot size.
  89. img.dotSize = int(nh / fh)
  90. // Save everything, making the actual width smaller by 1 dot to account
  91. // for spacing between digits.
  92. img.numWidth = int(nw)
  93. img.numHeight = int(nh) - img.dotSize
  94. }
  95. func (img *Image) drawHorizLine(color image.Color, fromX, toX, y int) {
  96. for x := fromX; x <= toX; x++ {
  97. img.Set(x, y, color)
  98. }
  99. }
  100. func (img *Image) drawCircle(color image.Color, x, y, radius int) {
  101. f := 1 - radius
  102. dfx := 1
  103. dfy := -2 * radius
  104. xx := 0
  105. yy := radius
  106. img.Set(x, y+radius, color)
  107. img.Set(x, y-radius, color)
  108. img.drawHorizLine(color, x-radius, x+radius, y)
  109. for xx < yy {
  110. if f >= 0 {
  111. yy--
  112. dfy += 2
  113. f += dfy
  114. }
  115. xx++
  116. dfx += 2
  117. f += dfx
  118. img.drawHorizLine(color, x-xx, x+xx, y+yy)
  119. img.drawHorizLine(color, x-xx, x+xx, y-yy)
  120. img.drawHorizLine(color, x-yy, x+yy, y+xx)
  121. img.drawHorizLine(color, x-yy, x+yy, y-xx)
  122. }
  123. }
  124. func (img *Image) fillWithCircles(n, maxradius int) {
  125. color := img.primaryColor
  126. maxx := img.Bounds().Max.X
  127. maxy := img.Bounds().Max.Y
  128. for i := 0; i < n; i++ {
  129. setRandomBrightness(&color, 255)
  130. r := rnd(1, maxradius)
  131. img.drawCircle(color, rnd(r, maxx-r), rnd(r, maxy-r), r)
  132. }
  133. }
  134. func (img *Image) strikeThrough() {
  135. r := 0
  136. maxx := img.Bounds().Max.X
  137. maxy := img.Bounds().Max.Y
  138. y := rnd(maxy/3, maxy-maxy/3)
  139. for x := 0; x < maxx; x += r {
  140. r = rnd(1, img.dotSize/2-1)
  141. y += rnd(-img.dotSize/2, img.dotSize/2)
  142. if y <= 0 || y >= maxy {
  143. y = rnd(maxy/3, maxy-maxy/3)
  144. }
  145. img.drawCircle(img.primaryColor, x, y, r)
  146. }
  147. }
  148. func (img *Image) drawDigit(digit []byte, x, y int) {
  149. skf := rand.Float64() * float64(rnd(-maxSkew, maxSkew))
  150. xs := float64(x)
  151. minr := img.dotSize / 2 // minumum radius
  152. maxr := img.dotSize/2 + img.dotSize/4 // maximum radius
  153. y += rnd(-minr, minr)
  154. for yy := 0; yy < fontHeight; yy++ {
  155. for xx := 0; xx < fontWidth; xx++ {
  156. if digit[yy*fontWidth+xx] != blackChar {
  157. continue
  158. }
  159. // Introduce random variations.
  160. or := rnd(minr, maxr)
  161. ox := x + (xx * img.dotSize) + rnd(0, or/2)
  162. oy := y + (yy * img.dotSize) + rnd(0, or/2)
  163. img.drawCircle(img.primaryColor, ox, oy, or)
  164. }
  165. xs += skf
  166. x = int(xs)
  167. }
  168. }
  169. func setRandomBrightness(c *image.NRGBAColor, max uint8) {
  170. minc := min3(c.R, c.G, c.B)
  171. maxc := max3(c.R, c.G, c.B)
  172. if maxc > max {
  173. return
  174. }
  175. n := rand.Intn(int(max-maxc)) - int(minc)
  176. c.R = uint8(int(c.R) + n)
  177. c.G = uint8(int(c.G) + n)
  178. c.B = uint8(int(c.B) + n)
  179. }
  180. func min3(x, y, z uint8) (o uint8) {
  181. o = x
  182. if y < o {
  183. o = y
  184. }
  185. if z < o {
  186. o = z
  187. }
  188. return
  189. }
  190. func max3(x, y, z uint8) (o uint8) {
  191. o = x
  192. if y > o {
  193. o = y
  194. }
  195. if z > o {
  196. o = z
  197. }
  198. return
  199. }
  200. // rnd returns a random number in range [from, to].
  201. func rnd(from, to int) int {
  202. return rand.Intn(to+1-from) + from
  203. }