all repos — captcha @ master

Go package captcha implements generation and verification of image and audio CAPTCHAs.

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	if m.dotSize < 1 {
133		m.dotSize = 1
134	}
135	// Save everything, making the actual width smaller by 1 dot to account
136	// for spacing between digits.
137	m.numWidth = int(nw) - m.dotSize
138	m.numHeight = int(nh)
139}
140
141func (m *Image) drawHorizLine(fromX, toX, y int, colorIdx uint8) {
142	for x := fromX; x <= toX; x++ {
143		m.SetColorIndex(x, y, colorIdx)
144	}
145}
146
147func (m *Image) drawCircle(x, y, radius int, colorIdx uint8) {
148	f := 1 - radius
149	dfx := 1
150	dfy := -2 * radius
151	xo := 0
152	yo := radius
153
154	m.SetColorIndex(x, y+radius, colorIdx)
155	m.SetColorIndex(x, y-radius, colorIdx)
156	m.drawHorizLine(x-radius, x+radius, y, colorIdx)
157
158	for xo < yo {
159		if f >= 0 {
160			yo--
161			dfy += 2
162			f += dfy
163		}
164		xo++
165		dfx += 2
166		f += dfx
167		m.drawHorizLine(x-xo, x+xo, y+yo, colorIdx)
168		m.drawHorizLine(x-xo, x+xo, y-yo, colorIdx)
169		m.drawHorizLine(x-yo, x+yo, y+xo, colorIdx)
170		m.drawHorizLine(x-yo, x+yo, y-xo, colorIdx)
171	}
172}
173
174func (m *Image) fillWithCircles(n, maxradius int) {
175	maxx := m.Bounds().Max.X
176	maxy := m.Bounds().Max.Y
177	for i := 0; i < n; i++ {
178		colorIdx := uint8(m.rng.Int(1, circleCount-1))
179		r := m.rng.Int(1, maxradius)
180		m.drawCircle(m.rng.Int(r, maxx-r), m.rng.Int(r, maxy-r), r, colorIdx)
181	}
182}
183
184func (m *Image) strikeThrough() {
185	maxx := m.Bounds().Max.X
186	maxy := m.Bounds().Max.Y
187	y := m.rng.Int(maxy/3, maxy-maxy/3)
188	amplitude := m.rng.Float(5, 20)
189	period := m.rng.Float(80, 180)
190	dx := 2.0 * math.Pi / period
191	for x := 0; x < maxx; x++ {
192		xo := amplitude * math.Cos(float64(y)*dx)
193		yo := amplitude * math.Sin(float64(x)*dx)
194		for yn := 0; yn < m.dotSize; yn++ {
195			r := m.rng.Int(0, m.dotSize)
196			m.drawCircle(x+int(xo), y+int(yo)+(yn*m.dotSize), r/2, 1)
197		}
198	}
199}
200
201func (m *Image) drawDigit(digit []byte, x, y int) {
202	skf := m.rng.Float(-maxSkew, maxSkew)
203	xs := float64(x)
204	r := m.dotSize / 2
205	y += m.rng.Int(-r, r)
206	for yo := 0; yo < fontHeight; yo++ {
207		for xo := 0; xo < fontWidth; xo++ {
208			if digit[yo*fontWidth+xo] != blackChar {
209				continue
210			}
211			m.drawCircle(x+xo*m.dotSize, y+yo*m.dotSize, r, 1)
212		}
213		xs += skf
214		x = int(xs)
215	}
216}
217
218func (m *Image) distort(amplude float64, period float64) {
219	w := m.Bounds().Max.X
220	h := m.Bounds().Max.Y
221
222	oldm := m.Paletted
223	newm := image.NewPaletted(image.Rect(0, 0, w, h), oldm.Palette)
224
225	dx := 2.0 * math.Pi / period
226	for x := 0; x < w; x++ {
227		for y := 0; y < h; y++ {
228			xo := amplude * math.Sin(float64(y)*dx)
229			yo := amplude * math.Cos(float64(x)*dx)
230			newm.SetColorIndex(x, y, oldm.ColorIndexAt(x+int(xo), y+int(yo)))
231		}
232	}
233	m.Paletted = newm
234}
235
236func (m *Image) randomBrightness(c color.RGBA, max uint8) color.RGBA {
237	minc := min3(c.R, c.G, c.B)
238	maxc := max3(c.R, c.G, c.B)
239	if maxc > max {
240		return c
241	}
242	n := m.rng.Intn(int(max-maxc)) - int(minc)
243	return color.RGBA{
244		uint8(int(c.R) + n),
245		uint8(int(c.G) + n),
246		uint8(int(c.B) + n),
247		uint8(c.A),
248	}
249}
250
251func min3(x, y, z uint8) (m uint8) {
252	m = x
253	if y < m {
254		m = y
255	}
256	if z < m {
257		m = z
258	}
259	return
260}
261
262func max3(x, y, z uint8) (m uint8) {
263	m = x
264	if y > m {
265		m = y
266	}
267	if z > m {
268		m = z
269	}
270	return
271}