item_digit.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package base64Captcha
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "fmt"
  6. "image"
  7. "image/color"
  8. "image/png"
  9. "io"
  10. "math"
  11. "math/rand"
  12. )
  13. const (
  14. digitFontWidth = 11
  15. digitFontHeight = 18
  16. digitFontBlackChar = 1
  17. )
  18. // ItemDigit digits captcha Struct
  19. type ItemDigit struct {
  20. width int
  21. height int
  22. *image.Paletted
  23. dotSize int
  24. dotCount int
  25. maxSkew float64
  26. //rng siprng
  27. }
  28. //NewItemDigit create a instance of item-digit
  29. func NewItemDigit(width int, height int, dotCount int, maxSkew float64) *ItemDigit {
  30. itemDigit := &ItemDigit{width: width, height: height, dotCount: dotCount, maxSkew: maxSkew}
  31. //init image.Paletted
  32. itemDigit.Paletted = image.NewPaletted(image.Rect(0, 0, width, height), createRandPaletteColors(dotCount))
  33. return itemDigit
  34. }
  35. func createRandPaletteColors(dotCount int) color.Palette {
  36. p := make([]color.Color, dotCount+1)
  37. // Transparent color.
  38. p[0] = color.RGBA{0xFF, 0xFF, 0xFF, 0x00}
  39. // Primary color.
  40. prim := color.RGBA{
  41. uint8(rand.Intn(129)),
  42. uint8(rand.Intn(129)),
  43. uint8(rand.Intn(129)),
  44. 0xFF,
  45. }
  46. if dotCount == 0 {
  47. p[0] = prim
  48. return p
  49. }
  50. p[1] = prim
  51. // Circle colors.
  52. for i := 2; i <= dotCount; i++ {
  53. p[i] = randomBrightness(prim, 255)
  54. }
  55. return p
  56. }
  57. func (m *ItemDigit) calculateSizes(width, height, ncount int) {
  58. // Goal: fit all digits inside the image.
  59. var border int
  60. if width > height {
  61. border = height / 4
  62. } else {
  63. border = width / 4
  64. }
  65. // Convert everything to floats for calculations.
  66. w := float64(width - border*2)
  67. h := float64(height - border*2)
  68. // fw takes into account 1-dot spacing between digits.
  69. fw := float64(digitFontWidth + 1)
  70. fh := float64(digitFontHeight)
  71. nc := float64(ncount)
  72. // Calculate the width of a single digit taking into account only the
  73. // width of the image.
  74. nw := w / nc
  75. // Calculate the height of a digit from this width.
  76. nh := nw * fh / fw
  77. // Digit too high?
  78. if nh > h {
  79. // Fit digits based on height.
  80. nh = h
  81. nw = fw / fh * nh
  82. }
  83. // Calculate dot size.
  84. m.dotSize = int(nh / fh)
  85. if m.dotSize < 1 {
  86. m.dotSize = 1
  87. }
  88. // Save everything, making the actual width smaller by 1 dot to account
  89. // for spacing between digits.
  90. m.width = int(nw) - m.dotSize
  91. m.height = int(nh)
  92. }
  93. func (m *ItemDigit) drawHorizLine(fromX, toX, y int, colorIdx uint8) {
  94. for x := fromX; x <= toX; x++ {
  95. m.SetColorIndex(x, y, colorIdx)
  96. }
  97. }
  98. func (m *ItemDigit) drawCircle(x, y, radius int, colorIdx uint8) {
  99. f := 1 - radius
  100. dfx := 1
  101. dfy := -2 * radius
  102. xo := 0
  103. yo := radius
  104. m.SetColorIndex(x, y+radius, colorIdx)
  105. m.SetColorIndex(x, y-radius, colorIdx)
  106. m.drawHorizLine(x-radius, x+radius, y, colorIdx)
  107. for xo < yo {
  108. if f >= 0 {
  109. yo--
  110. dfy += 2
  111. f += dfy
  112. }
  113. xo++
  114. dfx += 2
  115. f += dfx
  116. m.drawHorizLine(x-xo, x+xo, y+yo, colorIdx)
  117. m.drawHorizLine(x-xo, x+xo, y-yo, colorIdx)
  118. m.drawHorizLine(x-yo, x+yo, y+xo, colorIdx)
  119. m.drawHorizLine(x-yo, x+yo, y-xo, colorIdx)
  120. }
  121. }
  122. func (m *ItemDigit) fillWithCircles(n, maxradius int) {
  123. maxx := m.Bounds().Max.X
  124. maxy := m.Bounds().Max.Y
  125. for i := 0; i < n; i++ {
  126. //colorIdx := uint8(m.rng.Int(1, m.dotCount-1))
  127. colorIdx := uint8(randIntRange(1, m.dotCount-1))
  128. //r := m.rng.Int(1, maxradius)
  129. r := randIntRange(1, maxradius)
  130. //m.drawCircle(m.rng.Int(r, maxx-r), m.rng.Int(r, maxy-r), r, colorIdx)
  131. m.drawCircle(randIntRange(r, maxx-r), randIntRange(r, maxy-r), r, colorIdx)
  132. }
  133. }
  134. func (m *ItemDigit) strikeThrough() {
  135. maxx := m.Bounds().Max.X
  136. maxy := m.Bounds().Max.Y
  137. y := randIntRange(maxy/3, maxy-maxy/3)
  138. amplitude := randFloat64Range(5, 20)
  139. period := randFloat64Range(80, 180)
  140. dx := 2.0 * math.Pi / period
  141. for x := 0; x < maxx; x++ {
  142. xo := amplitude * math.Cos(float64(y)*dx)
  143. yo := amplitude * math.Sin(float64(x)*dx)
  144. for yn := 0; yn < m.dotSize; yn++ {
  145. //r := m.rng.Int(0, m.dotSize)
  146. r := rand.Intn(m.dotSize)
  147. m.drawCircle(x+int(xo), y+int(yo)+(yn*m.dotSize), r/2, 1)
  148. }
  149. }
  150. }
  151. //draw digit
  152. func (m *ItemDigit) drawDigit(digit []byte, x, y int) {
  153. skf := randFloat64Range(-m.maxSkew, m.maxSkew)
  154. xs := float64(x)
  155. r := m.dotSize / 2
  156. y += randIntRange(-r, r)
  157. for yo := 0; yo < digitFontHeight; yo++ {
  158. for xo := 0; xo < digitFontWidth; xo++ {
  159. if digit[yo*digitFontWidth+xo] != digitFontBlackChar {
  160. continue
  161. }
  162. m.drawCircle(x+xo*m.dotSize, y+yo*m.dotSize, r, 1)
  163. }
  164. xs += skf
  165. x = int(xs)
  166. }
  167. }
  168. func (m *ItemDigit) distort(amplude float64, period float64) {
  169. w := m.Bounds().Max.X
  170. h := m.Bounds().Max.Y
  171. oldm := m.Paletted
  172. newm := image.NewPaletted(image.Rect(0, 0, w, h), oldm.Palette)
  173. dx := 2.0 * math.Pi / period
  174. for x := 0; x < w; x++ {
  175. for y := 0; y < h; y++ {
  176. xo := amplude * math.Sin(float64(y)*dx)
  177. yo := amplude * math.Cos(float64(x)*dx)
  178. newm.SetColorIndex(x, y, oldm.ColorIndexAt(x+int(xo), y+int(yo)))
  179. }
  180. }
  181. m.Paletted = newm
  182. }
  183. func randomBrightness(c color.RGBA, max uint8) color.RGBA {
  184. minc := min3(c.R, c.G, c.B)
  185. maxc := max3(c.R, c.G, c.B)
  186. if maxc > max {
  187. return c
  188. }
  189. n := rand.Intn(int(max-maxc)) - int(minc)
  190. return color.RGBA{
  191. uint8(int(c.R) + n),
  192. uint8(int(c.G) + n),
  193. uint8(int(c.B) + n),
  194. uint8(c.A),
  195. }
  196. }
  197. func min3(x, y, z uint8) (m uint8) {
  198. m = x
  199. if y < m {
  200. m = y
  201. }
  202. if z < m {
  203. m = z
  204. }
  205. return
  206. }
  207. func max3(x, y, z uint8) (m uint8) {
  208. m = x
  209. if y > m {
  210. m = y
  211. }
  212. if z > m {
  213. m = z
  214. }
  215. return
  216. }
  217. // EncodeBinary encodes an image to PNG and returns a byte slice.
  218. func (m *ItemDigit) EncodeBinary() []byte {
  219. var buf bytes.Buffer
  220. if err := png.Encode(&buf, m.Paletted); err != nil {
  221. panic(err.Error())
  222. }
  223. return buf.Bytes()
  224. }
  225. // WriteTo writes captcha character in png format into the given io.Writer, and
  226. // returns the number of bytes written and an error if any.
  227. func (m *ItemDigit) WriteTo(w io.Writer) (int64, error) {
  228. n, err := w.Write(m.EncodeBinary())
  229. return int64(n), err
  230. }
  231. // EncodeB64string encodes an image to base64 string
  232. func (m *ItemDigit) EncodeB64string() string {
  233. return fmt.Sprintf("data:%s;base64,%s", MimeTypeImage, base64.StdEncoding.EncodeToString(m.EncodeBinary()))
  234. }