Browse Source

Move image-related functions into image.go.

Dmitry Chestnykh 14 years ago
parent
commit
364b4304db
4 changed files with 38 additions and 27 deletions
  1. 2 1
      Makefile
  2. 0 25
      captcha.go
  3. 1 1
      cmd/main.go
  4. 35 0
      image.go

+ 2 - 1
Makefile

@@ -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
 

+ 0 - 25
captcha.go

@@ -2,7 +2,6 @@ package captcha
 
 import (
 	"bytes"
-	"image"
 	"image/png"
 	"os"
 	"rand"
@@ -43,24 +42,6 @@ func newStore() *storage {
 
 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())
 }
@@ -76,12 +57,6 @@ func randomNumbers() []byte {
 	return n
 }
 
-func Encode(w io.Writer) (numbers []byte, err os.Error) {
-	numbers = randomNumbers()
-	err = png.Encode(w, NewImage(numbers))
-	return
-}
-
 func New() string {
 	ns := randomNumbers()
 	id := uniuri.New()

+ 1 - 1
cmd/main.go

@@ -6,5 +6,5 @@ import (
 )
 
 func main() {
-	captcha.Encode(os.Stdout)
+	captcha.EncodeNewImage(os.Stdout)
 }

+ 35 - 0
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
+}
+