image.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 for 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 slice of numbers, where each number must be in range 0-9.
  28. func NewImage(numbers []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(numbers))
  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(numbers) - 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 numbers
  47. for _, n := range numbers {
  48. img.drawNumber(font[n], x, y)
  49. x += img.numWidth + img.dotSize
  50. }
  51. // Draw strike-through line
  52. img.strikeThrough()
  53. return img
  54. }
  55. // NewRandomImage generates a sequence of random numbers with the given length,
  56. // and returns a new captcha image of the given width and height with generated
  57. // numbers printed on it, and the sequence of numbers itself.
  58. func NewRandomImage(length, width, height int) (img *Image, numbers []byte) {
  59. numbers = randomNumbers(length)
  60. img = NewImage(numbers, width, height)
  61. return
  62. }
  63. // PNGEncode writes captcha image in PNG format into the given writer.
  64. func (img *Image) PNGEncode(w io.Writer) os.Error {
  65. return png.Encode(w, img)
  66. }
  67. func (img *Image) calculateSizes(width, height, ncount int) {
  68. // Goal: fit all numbers inside the image.
  69. var border int
  70. if width > height {
  71. border = height / 5
  72. } else {
  73. border = width / 5
  74. }
  75. // Convert everything to floats for calculations
  76. w := float64(width-border*2)
  77. h := float64(height-border*2)
  78. // fw takes into account 1-dot spacing between numbers
  79. fw := float64(fontWidth) + 1
  80. fh := float64(fontHeight)
  81. nc := float64(ncount)
  82. // Calculate the width of a single number taking into account only the
  83. // width of the image
  84. nw := w / nc
  85. // Calculate the height of a number from this width
  86. nh := nw * fh / fw
  87. // Number height too large?
  88. if nh > h {
  89. // Fit numbers based on height
  90. nh = h
  91. nw = fw / fh * nh
  92. }
  93. // Calculate dot size
  94. img.dotSize = int(nh / fh)
  95. // Save everything, making the actual width smaller by 1 dot to account
  96. // for spacing between numbers
  97. img.numWidth = int(nw)
  98. img.numHeight = int(nh) - img.dotSize
  99. }
  100. func (img *Image) drawHorizLine(color image.Color, fromX, toX, y int) {
  101. for x := fromX; x <= toX; x++ {
  102. img.Set(x, y, color)
  103. }
  104. }
  105. func (img *Image) drawCircle(color image.Color, x, y, radius int) {
  106. f := 1 - radius
  107. dfx := 1
  108. dfy := -2 * radius
  109. xx := 0
  110. yy := radius
  111. img.Set(x, y+radius, color)
  112. img.Set(x, y-radius, color)
  113. img.drawHorizLine(color, x-radius, x+radius, y)
  114. for xx < yy {
  115. if f >= 0 {
  116. yy--
  117. dfy += 2
  118. f += dfy
  119. }
  120. xx++
  121. dfx += 2
  122. f += dfx
  123. img.drawHorizLine(color, x-xx, x+xx, y+yy)
  124. img.drawHorizLine(color, x-xx, x+xx, y-yy)
  125. img.drawHorizLine(color, x-yy, x+yy, y+xx)
  126. img.drawHorizLine(color, x-yy, x+yy, y-xx)
  127. }
  128. }
  129. func (img *Image) fillWithCircles(n, maxradius int) {
  130. color := img.primaryColor
  131. maxx := img.Bounds().Max.X
  132. maxy := img.Bounds().Max.Y
  133. for i := 0; i < n; i++ {
  134. setRandomBrightness(&color, 255)
  135. r := rnd(1, maxradius)
  136. img.drawCircle(color, rnd(r, maxx-r), rnd(r, maxy-r), r)
  137. }
  138. }
  139. func (img *Image) strikeThrough() {
  140. r := 0
  141. maxx := img.Bounds().Max.X
  142. maxy := img.Bounds().Max.Y
  143. y := rnd(maxy/3, maxy-maxy/3)
  144. for x := 0; x < maxx; x += r {
  145. r = rnd(1, img.dotSize/2-1)
  146. y += rnd(-img.dotSize/2, img.dotSize/2)
  147. if y <= 0 || y >= maxy {
  148. y = rnd(maxy/3, maxy-maxy/3)
  149. }
  150. img.drawCircle(img.primaryColor, x, y, r)
  151. }
  152. }
  153. func (img *Image) drawNumber(number []byte, x, y int) {
  154. skf := rand.Float64() * float64(rnd(-maxSkew, maxSkew))
  155. xs := float64(x)
  156. minr := img.dotSize / 2 // minumum radius
  157. maxr := img.dotSize/2 + img.dotSize/4 // maximum radius
  158. y += rnd(-minr, minr)
  159. for yy := 0; yy < fontHeight; yy++ {
  160. for xx := 0; xx < fontWidth; xx++ {
  161. if number[yy*fontWidth+xx] != blackChar {
  162. continue
  163. }
  164. // introduce random variations
  165. or := rnd(minr, maxr)
  166. ox := x + (xx * img.dotSize) + rnd(0, or/2)
  167. oy := y + (yy * img.dotSize) + rnd(0, or/2)
  168. img.drawCircle(img.primaryColor, ox, oy, or)
  169. }
  170. xs += skf
  171. x = int(xs)
  172. }
  173. }
  174. func setRandomBrightness(c *image.NRGBAColor, max uint8) {
  175. minc := min3(c.R, c.G, c.B)
  176. maxc := max3(c.R, c.G, c.B)
  177. if maxc > max {
  178. return
  179. }
  180. n := rand.Intn(int(max-maxc)) - int(minc)
  181. c.R = uint8(int(c.R) + n)
  182. c.G = uint8(int(c.G) + n)
  183. c.B = uint8(int(c.B) + n)
  184. }
  185. func min3(x, y, z uint8) (o uint8) {
  186. o = x
  187. if y < o {
  188. o = y
  189. }
  190. if z < o {
  191. o = z
  192. }
  193. return
  194. }
  195. func max3(x, y, z uint8) (o uint8) {
  196. o = x
  197. if y > o {
  198. o = y
  199. }
  200. if z > o {
  201. o = z
  202. }
  203. return
  204. }
  205. // rnd returns a random number in range [from, to].
  206. func rnd(from, to int) int {
  207. return rand.Intn(to+1-from) + from
  208. }