all repos — captcha @ 364b4304db2a30e1940ea73035bf1b8c4f4f68bf

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

Move image-related functions into image.go.
Dmitry Chestnykh dmitry@codingrobots.com
Wed, 20 Apr 2011 03:14:29 +0200
commit

364b4304db2a30e1940ea73035bf1b8c4f4f68bf

parent

b04efdccea61eed956f4e924eb31848a581b790b

4 files changed, 38 insertions(+), 27 deletions(-)

jump to
M MakefileMakefile

@@ -4,7 +4,8 @@ TARG=github.com/dchest/captcha

GOFILES=\ captcha.go\ draw.go\ - font.go + font.go\ + image.go include $(GOROOT)/src/Make.pkg
M captcha.gocaptcha.go

@@ -2,7 +2,6 @@ package captcha

import ( "bytes" - "image" "image/png" "os" "rand"

@@ -43,24 +42,6 @@ }

var store = newStore() -func NewImage(numbers []byte) *image.NRGBA { - w := numberWidth * (dotSize + 3) * len(numbers) - h := numberHeight * (dotSize + 5) - img := image.NewNRGBA(w, h) - color := image.NRGBAColor{uint8(rand.Intn(50)), uint8(rand.Intn(50)), uint8(rand.Intn(128)), 0xFF} - fillWithCircles(img, color, 40, 4) - x := rand.Intn(dotSize) - y := 0 - setRandomBrightness(&color, 180) - for _, n := range numbers { - y = rand.Intn(dotSize * 4) - drawNumber(img, font[n], x, y, color) - x += dotSize*numberWidth + rand.Intn(maxSkew) + 8 - } - drawCirclesLine(img, color) - return img -} - func init() { rand.Seed(time.Seconds()) }

@@ -74,12 +55,6 @@ for i := range n {

n[i] %= 10 } return n -} - -func Encode(w io.Writer) (numbers []byte, err os.Error) { - numbers = randomNumbers() - err = png.Encode(w, NewImage(numbers)) - return } func New() string {
M cmd/main.gocmd/main.go

@@ -6,5 +6,5 @@ "os"

) func main() { - captcha.Encode(os.Stdout) + captcha.EncodeNewImage(os.Stdout) }
A image.go

@@ -0,0 +1,35 @@

+package captcha + +import ( + "image" + "image/png" + "io" + "os" + "rand" +) + + +func NewImage(numbers []byte) *image.NRGBA { + w := numberWidth * (dotSize + 3) * len(numbers) + h := numberHeight * (dotSize + 5) + img := image.NewNRGBA(w, h) + color := image.NRGBAColor{uint8(rand.Intn(50)), uint8(rand.Intn(50)), uint8(rand.Intn(128)), 0xFF} + fillWithCircles(img, color, 40, 4) + x := rand.Intn(dotSize) + y := 0 + setRandomBrightness(&color, 180) + for _, n := range numbers { + y = rand.Intn(dotSize * 4) + drawNumber(img, font[n], x, y, color) + x += dotSize*numberWidth + rand.Intn(maxSkew) + 8 + } + drawCirclesLine(img, color) + return img +} + +func EncodeNewImage(w io.Writer) (numbers []byte, err os.Error) { + numbers = randomNumbers() + err = png.Encode(w, NewImage(numbers)) + return +} +