Move image-related functions into image.go.
Dmitry Chestnykh dmitry@codingrobots.com
Wed, 20 Apr 2011 03:14:29 +0200
4 files changed,
38 insertions(+),
27 deletions(-)
M
captcha.go
→
captcha.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.go
→
cmd/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 +} +