image.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package captcha
  2. import (
  3. "image"
  4. "image/png"
  5. "io"
  6. "os"
  7. "rand"
  8. )
  9. const (
  10. maxSkew = 3
  11. )
  12. type CaptchaImage struct {
  13. *image.NRGBA
  14. primaryColor image.NRGBAColor
  15. numberWidth int
  16. numberHeight int
  17. dotSize int
  18. }
  19. func (img *CaptchaImage) calculateSizes(width, height, ncount int) {
  20. // Goal: fit all numbers into the image.
  21. // Convert everything to floats for calculations.
  22. w := float64(width)
  23. h := float64(height)
  24. // fontWidth includes 1-dot spacing between numbers
  25. fw := float64(fontWidth) + 1
  26. fh := float64(fontHeight)
  27. nc := float64(ncount)
  28. // Calculate width of a sigle number if we only take into
  29. // account the width
  30. nw := w / nc
  31. // Calculate the number height from this width
  32. nh := nw * fh / fw
  33. // Number height too large?
  34. if nh > h {
  35. // Fit numbers based on height
  36. nh = h
  37. nw = fw/fh * nh
  38. }
  39. // Calculate dot size
  40. img.dotSize = int(nh / fh)
  41. // Save everything, making actual width smaller by 1 dot,
  42. // to account for spacing between numbers
  43. img.numberWidth = int(nw)
  44. img.numberHeight = int(nh) - img.dotSize
  45. }
  46. func NewImage(numbers []byte, width, height int) *CaptchaImage {
  47. img := new(CaptchaImage)
  48. img.NRGBA = image.NewNRGBA(width, height)
  49. img.primaryColor = image.NRGBAColor{uint8(rand.Intn(50)), uint8(rand.Intn(50)), uint8(rand.Intn(128)), 0xFF}
  50. // We need some space, so calculate border
  51. var border int
  52. if width > height {
  53. border = height/5
  54. } else {
  55. border = width/5
  56. }
  57. bwidth := width-border*2
  58. bheight := height-border*2
  59. img.calculateSizes(bwidth, bheight, len(numbers))
  60. // Center numbers in image
  61. x := width/2 - (img.numberWidth*len(numbers))/2 - img.dotSize
  62. y := height/2 - img.numberHeight/2 + img.dotSize/2
  63. setRandomBrightness(&img.primaryColor, 180)
  64. for _, n := range numbers {
  65. //y = rand.Intn(dotSize * 4)
  66. img.drawNumber(font[n], x, y)
  67. x += img.numberWidth + img.dotSize
  68. }
  69. //img.strikeThrough(img.primaryColor)
  70. return img
  71. }
  72. func NewRandomImage(width, height int) (img *CaptchaImage, numbers []byte) {
  73. numbers = randomNumbers()
  74. img = NewImage(numbers, width, height)
  75. return
  76. }
  77. func (img *CaptchaImage) PNGEncode(w io.Writer) os.Error {
  78. return png.Encode(w, img)
  79. }
  80. func (img *CaptchaImage) drawHorizLine(color image.Color, fromX, toX, y int) {
  81. for x := fromX; x <= toX; x++ {
  82. img.Set(x, y, color)
  83. }
  84. }
  85. func (img *CaptchaImage) drawCircle(color image.Color, x, y, radius int) {
  86. f := 1 - radius
  87. dfx := 1
  88. dfy := -2 * radius
  89. xx := 0
  90. yy := radius
  91. img.Set(x, y+radius, color)
  92. img.Set(x, y-radius, color)
  93. img.drawHorizLine(color, x-radius, x+radius, y)
  94. for xx < yy {
  95. if f >= 0 {
  96. yy--
  97. dfy += 2
  98. f += dfy
  99. }
  100. xx++
  101. dfx += 2
  102. f += dfx
  103. img.drawHorizLine(color, x-xx, x+xx, y+yy)
  104. img.drawHorizLine(color, x-xx, x+xx, y-yy)
  105. img.drawHorizLine(color, x-yy, x+yy, y+xx)
  106. img.drawHorizLine(color, x-yy, x+yy, y-xx)
  107. }
  108. }
  109. func min3(x, y, z uint8) (o uint8) {
  110. o = x
  111. if y < o {
  112. o = y
  113. }
  114. if z < o {
  115. o = z
  116. }
  117. return
  118. }
  119. func max3(x, y, z uint8) (o uint8) {
  120. o = x
  121. if y > o {
  122. o = y
  123. }
  124. if z > o {
  125. o = z
  126. }
  127. return
  128. }
  129. func setRandomBrightness(c *image.NRGBAColor, max uint8) {
  130. minc := min3(c.R, c.G, c.B)
  131. maxc := max3(c.R, c.G, c.B)
  132. if maxc > max {
  133. return
  134. }
  135. n := rand.Intn(int(max-maxc)) - int(minc)
  136. c.R = uint8(int(c.R) + n)
  137. c.G = uint8(int(c.G) + n)
  138. c.B = uint8(int(c.B) + n)
  139. }
  140. func rnd(from, to int) int {
  141. return rand.Intn(to+1-from) + from
  142. }
  143. func (img *CaptchaImage) fillWithCircles(color image.NRGBAColor, n, maxradius int) {
  144. maxx := img.Bounds().Max.X
  145. maxy := img.Bounds().Max.Y
  146. for i := 0; i < n; i++ {
  147. setRandomBrightness(&color, 255)
  148. r := rnd(1, maxradius)
  149. img.drawCircle(color, rnd(r, maxx), rnd(r, maxy), r)
  150. }
  151. }
  152. // func (img *CaptchaImage) strikeThrough(color image.Color) {
  153. // r := 0
  154. // maxx := img.Bounds().Max.X
  155. // maxy := img.Bounds().Max.Y
  156. // y := rnd(maxy/3, maxy-maxy/3)
  157. // for x := 0; x < maxx; x += r {
  158. // r = rnd(1, dotSize/2-1)
  159. // y += rnd(-2, 2)
  160. // if y <= 0 || y >= maxy {
  161. // y = rnd(maxy/3, maxy-maxy/3)
  162. // }
  163. // img.drawCircle(color, x, y, r)
  164. // }
  165. // }
  166. func (img *CaptchaImage) drawNumber(number []byte, x, y int) {
  167. //skf := rand.Intn(maxSkew) - maxSkew/2
  168. //if skf < 0 {
  169. // x -= skf * numberHeight
  170. //}
  171. //d := img.numberWidth / fontWidth // number height is ignored
  172. //println(img.numberWidth)
  173. minr := img.dotSize/2 // minumum radius
  174. maxr := img.dotSize // maximum radius
  175. // x += srad
  176. // y += srad
  177. for yy := 0; yy < fontHeight; yy++ {
  178. for xx := 0; xx < fontWidth; xx++ {
  179. if number[yy*fontWidth+xx] != 1 {
  180. continue
  181. }
  182. // introduce random variations
  183. or := rnd(minr, maxr)
  184. ox := x + (xx * maxr) //+ rnd(0, or/2)
  185. oy := y + (yy * maxr) //+ rnd(0, or/2)
  186. img.drawCircle(img.primaryColor, ox, oy, or)
  187. }
  188. //x += skf
  189. }
  190. }