all repos — captcha @ 5357b20fc91de2905938534e511feb7a9d2887b9

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

Rename CaptchaImage -> Image, CaptchaAudio -> Audio.
Dmitry Chestnykh dmitry@codingrobots.com
Sat, 23 Apr 2011 18:41:00 +0200
commit

5357b20fc91de2905938534e511feb7a9d2887b9

parent

82004bd901963399caa43f308230f2ccb8ae5cd0

2 files changed, 15 insertions(+), 15 deletions(-)

jump to
M audio.goaudio.go

@@ -133,11 +133,11 @@ }

endingBeepSound = changeSpeed(beepSound, 1.4) } -type CaptchaAudio struct { +type Audio struct { body *bytes.Buffer } -func NewAudio(numbers []byte) *CaptchaAudio { +func NewAudio(numbers []byte) *Audio { numsnd := make([][]byte, len(numbers)) nsdur := 0 for i, n := range numbers {

@@ -157,7 +157,7 @@ }

// Background noise bg := makeBackgroundSound(longestNumSndLen*len(numbers) + intdur) // -- - a := new(CaptchaAudio) + a := new(Audio) a.body = bytes.NewBuffer(nil) // Prelude, three beeps sil := makeSilence(sampleRate / 5)

@@ -179,7 +179,7 @@ return a

} // WriteTo writes captcha audio in WAVE format. -func (a *CaptchaAudio) WriteTo(w io.Writer) (n int64, err os.Error) { +func (a *Audio) WriteTo(w io.Writer) (n int64, err os.Error) { nn, err := w.Write(waveHeader) n = int64(nn) if err != nil {
M image.goimage.go

@@ -17,7 +17,7 @@

maxSkew = 2 ) -type CaptchaImage struct { +type Image struct { *image.NRGBA primaryColor image.NRGBAColor numWidth int

@@ -31,8 +31,8 @@ }

// NewImage returns a new captcha image of the given width and height with the // given slice of numbers, where each number must be in range 0-9. -func NewImage(numbers []byte, width, height int) *CaptchaImage { - img := new(CaptchaImage) +func NewImage(numbers []byte, width, height int) *Image { + img := new(Image) img.NRGBA = image.NewNRGBA(width, height) img.primaryColor = image.NRGBAColor{ uint8(rand.Intn(129)),

@@ -62,18 +62,18 @@

// NewRandomImage generates a sequence of random numbers with the given length, // and returns a new captcha image of the given width and height with generated // numbers printed on it, and the sequence of numbers itself. -func NewRandomImage(length, width, height int) (img *CaptchaImage, numbers []byte) { +func NewRandomImage(length, width, height int) (img *Image, numbers []byte) { numbers = randomNumbers(length) img = NewImage(numbers, width, height) return } // PNGEncode writes captcha image in PNG format into the given writer. -func (img *CaptchaImage) PNGEncode(w io.Writer) os.Error { +func (img *Image) PNGEncode(w io.Writer) os.Error { return png.Encode(w, img) } -func (img *CaptchaImage) calculateSizes(width, height, ncount int) { +func (img *Image) calculateSizes(width, height, ncount int) { // Goal: fit all numbers inside the image. var border int if width > height {

@@ -107,13 +107,13 @@ img.numWidth = int(nw)

img.numHeight = int(nh) - img.dotSize } -func (img *CaptchaImage) drawHorizLine(color image.Color, fromX, toX, y int) { +func (img *Image) drawHorizLine(color image.Color, fromX, toX, y int) { for x := fromX; x <= toX; x++ { img.Set(x, y, color) } } -func (img *CaptchaImage) drawCircle(color image.Color, x, y, radius int) { +func (img *Image) drawCircle(color image.Color, x, y, radius int) { f := 1 - radius dfx := 1 dfy := -2 * radius

@@ -140,7 +140,7 @@ img.drawHorizLine(color, x-yy, x+yy, y-xx)

} } -func (img *CaptchaImage) fillWithCircles(n, maxradius int) { +func (img *Image) fillWithCircles(n, maxradius int) { color := img.primaryColor maxx := img.Bounds().Max.X maxy := img.Bounds().Max.Y

@@ -151,7 +151,7 @@ img.drawCircle(color, rnd(r, maxx-r), rnd(r, maxy-r), r)

} } -func (img *CaptchaImage) strikeThrough() { +func (img *Image) strikeThrough() { r := 0 maxx := img.Bounds().Max.X maxy := img.Bounds().Max.Y

@@ -166,7 +166,7 @@ img.drawCircle(img.primaryColor, x, y, r)

} } -func (img *CaptchaImage) drawNumber(number []byte, x, y int) { +func (img *Image) drawNumber(number []byte, x, y int) { skf := rand.Float64() * float64(rnd(-maxSkew, maxSkew)) xs := float64(x) minr := img.dotSize / 2 // minumum radius