image.go (view raw)
1// Copyright 2011-2014 Dmitry Chestnykh. All rights reserved.
2// Use of this source code is governed by a MIT-style
3// license that can be found in the LICENSE file.
4
5package captcha
6
7import (
8 "bytes"
9 "image"
10 "image/color"
11 "image/png"
12 "io"
13 "math"
14)
15
16const (
17 // Standard width and height of a captcha image.
18 StdWidth = 240
19 StdHeight = 80
20 // Maximum absolute skew factor of a single digit.
21 maxSkew = 0.7
22 // Number of background circles.
23 circleCount = 20
24)
25
26type Image struct {
27 *image.Paletted
28 numWidth int
29 numHeight int
30 dotSize int
31 rng siprng
32}
33
34// NewImage returns a new captcha image of the given width and height with the
35// given digits, where each digit must be in range 0-9.
36func NewImage(id string, digits []byte, width, height int) *Image {
37 m := new(Image)
38
39 // Initialize PRNG.
40 m.rng.Seed(deriveSeed(imageSeedPurpose, id, digits))
41
42 m.Paletted = image.NewPaletted(image.Rect(0, 0, width, height), m.getRandomPalette())
43 m.calculateSizes(width, height, len(digits))
44 // Randomly position captcha inside the image.
45 maxx := width - (m.numWidth+m.dotSize)*len(digits) - m.dotSize
46 maxy := height - m.numHeight - m.dotSize*2
47 var border int
48 if width > height {
49 border = height / 5
50 } else {
51 border = width / 5
52 }
53 x := m.rng.Int(border, maxx-border)
54 y := m.rng.Int(border, maxy-border)
55 // Draw digits.
56 for _, n := range digits {
57 m.drawDigit(font[n], x, y)
58 x += m.numWidth + m.dotSize
59 }
60 // Draw strike-through line.
61 m.strikeThrough()
62 // Apply wave distortion.
63 m.distort(m.rng.Float(5, 10), m.rng.Float(100, 200))
64 // Fill image with random circles.
65 m.fillWithCircles(circleCount, m.dotSize)
66 return m
67}
68
69func (m *Image) getRandomPalette() color.Palette {
70 p := make([]color.Color, circleCount+1)
71 // Transparent color.
72 p[0] = color.RGBA{0xFF, 0xFF, 0xFF, 0x00}
73 // Primary color.
74 prim := color.RGBA{
75 uint8(m.rng.Intn(129)),
76 uint8(m.rng.Intn(129)),
77 uint8(m.rng.Intn(129)),
78 0xFF,
79 }
80 p[1] = prim
81 // Circle colors.
82 for i := 2; i <= circleCount; i++ {
83 p[i] = m.randomBrightness(prim, 255)
84 }
85 return p
86}
87
88// encodedPNG encodes an image to PNG and returns
89// the result as a byte slice.
90func (m *Image) encodedPNG() []byte {
91 var buf bytes.Buffer
92 if err := png.Encode(&buf, m.Paletted); err != nil {
93 panic(err.Error())
94 }
95 return buf.Bytes()
96}
97
98// WriteTo writes captcha image in PNG format into the given writer.
99func (m *Image) WriteTo(w io.Writer) (int64, error) {
100 n, err := w.Write(m.encodedPNG())
101 return int64(n), err
102}
103
104func (m *Image) calculateSizes(width, height, ncount int) {
105 // Goal: fit all digits inside the image.
106 var border int
107 if width > height {
108 border = height / 4
109 } else {
110 border = width / 4
111 }
112 // Convert everything to floats for calculations.
113 w := float64(width - border*2)
114 h := float64(height - border*2)
115 // fw takes into account 1-dot spacing between digits.
116 fw := float64(fontWidth + 1)
117 fh := float64(fontHeight)
118 nc := float64(ncount)
119 // Calculate the width of a single digit taking into account only the
120 // width of the image.
121 nw := w / nc
122 // Calculate the height of a digit from this width.
123 nh := nw * fh / fw
124 // Digit too high?
125 if nh > h {
126 // Fit digits based on height.
127 nh = h
128 nw = fw / fh * nh
129 }
130 // Calculate dot size.
131 m.dotSize = int(nh / fh)
132 // Save everything, making the actual width smaller by 1 dot to account
133 // for spacing between digits.
134 m.numWidth = int(nw) - m.dotSize
135 m.numHeight = int(nh)
136}
137
138func (m *Image) drawHorizLine(fromX, toX, y int, colorIdx uint8) {
139 for x := fromX; x <= toX; x++ {
140 m.SetColorIndex(x, y, colorIdx)
141 }
142}
143
144func (m *Image) drawCircle(x, y, radius int, colorIdx uint8) {
145 f := 1 - radius
146 dfx := 1
147 dfy := -2 * radius
148 xo := 0
149 yo := radius
150
151 m.SetColorIndex(x, y+radius, colorIdx)
152 m.SetColorIndex(x, y-radius, colorIdx)
153 m.drawHorizLine(x-radius, x+radius, y, colorIdx)
154
155 for xo < yo {
156 if f >= 0 {
157 yo--
158 dfy += 2
159 f += dfy
160 }
161 xo++
162 dfx += 2
163 f += dfx
164 m.drawHorizLine(x-xo, x+xo, y+yo, colorIdx)
165 m.drawHorizLine(x-xo, x+xo, y-yo, colorIdx)
166 m.drawHorizLine(x-yo, x+yo, y+xo, colorIdx)
167 m.drawHorizLine(x-yo, x+yo, y-xo, colorIdx)
168 }
169}
170
171func (m *Image) fillWithCircles(n, maxradius int) {
172 maxx := m.Bounds().Max.X
173 maxy := m.Bounds().Max.Y
174 for i := 0; i < n; i++ {
175 colorIdx := uint8(m.rng.Int(1, circleCount-1))
176 r := m.rng.Int(1, maxradius)
177 m.drawCircle(m.rng.Int(r, maxx-r), m.rng.Int(r, maxy-r), r, colorIdx)
178 }
179}
180
181func (m *Image) strikeThrough() {
182 maxx := m.Bounds().Max.X
183 maxy := m.Bounds().Max.Y
184 y := m.rng.Int(maxy/3, maxy-maxy/3)
185 amplitude := m.rng.Float(5, 20)
186 period := m.rng.Float(80, 180)
187 dx := 2.0 * math.Pi / period
188 for x := 0; x < maxx; x++ {
189 xo := amplitude * math.Cos(float64(y)*dx)
190 yo := amplitude * math.Sin(float64(x)*dx)
191 for yn := 0; yn < m.dotSize; yn++ {
192 r := m.rng.Int(0, m.dotSize)
193 m.drawCircle(x+int(xo), y+int(yo)+(yn*m.dotSize), r/2, 1)
194 }
195 }
196}
197
198func (m *Image) drawDigit(digit []byte, x, y int) {
199 skf := m.rng.Float(-maxSkew, maxSkew)
200 xs := float64(x)
201 r := m.dotSize / 2
202 y += m.rng.Int(-r, r)
203 for yo := 0; yo < fontHeight; yo++ {
204 for xo := 0; xo < fontWidth; xo++ {
205 if digit[yo*fontWidth+xo] != blackChar {
206 continue
207 }
208 m.drawCircle(x+xo*m.dotSize, y+yo*m.dotSize, r, 1)
209 }
210 xs += skf
211 x = int(xs)
212 }
213}
214
215func (m *Image) distort(amplude float64, period float64) {
216 w := m.Bounds().Max.X
217 h := m.Bounds().Max.Y
218
219 oldm := m.Paletted
220 newm := image.NewPaletted(image.Rect(0, 0, w, h), oldm.Palette)
221
222 dx := 2.0 * math.Pi / period
223 for x := 0; x < w; x++ {
224 for y := 0; y < h; y++ {
225 xo := amplude * math.Sin(float64(y)*dx)
226 yo := amplude * math.Cos(float64(x)*dx)
227 newm.SetColorIndex(x, y, oldm.ColorIndexAt(x+int(xo), y+int(yo)))
228 }
229 }
230 m.Paletted = newm
231}
232
233func (m *Image) randomBrightness(c color.RGBA, max uint8) color.RGBA {
234 minc := min3(c.R, c.G, c.B)
235 maxc := max3(c.R, c.G, c.B)
236 if maxc > max {
237 return c
238 }
239 n := m.rng.Intn(int(max-maxc)) - int(minc)
240 return color.RGBA{
241 uint8(int(c.R) + n),
242 uint8(int(c.G) + n),
243 uint8(int(c.B) + n),
244 uint8(c.A),
245 }
246}
247
248func min3(x, y, z uint8) (m uint8) {
249 m = x
250 if y < m {
251 m = y
252 }
253 if z < m {
254 m = z
255 }
256 return
257}
258
259func max3(x, y, z uint8) (m uint8) {
260 m = x
261 if y > m {
262 m = y
263 }
264 if z > m {
265 m = z
266 }
267 return
268}