Browse Source

Add line that strikes through digits.

Dmitry Chestnykh 14 years ago
parent
commit
b04efdccea
3 changed files with 50 additions and 33 deletions
  1. 3 2
      captcha.go
  2. 18 2
      draw.go
  3. 29 29
      font.go

+ 3 - 2
captcha.go

@@ -44,7 +44,7 @@ func newStore() *storage {
 var store = newStore()
 
 func NewImage(numbers []byte) *image.NRGBA {
-	w := numberWidth * (dotSize + 2) * len(numbers)
+	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}
@@ -55,8 +55,9 @@ func NewImage(numbers []byte) *image.NRGBA {
 	for _, n := range numbers {
 		y = rand.Intn(dotSize * 4)
 		drawNumber(img, font[n], x, y, color)
-		x += dotSize*numberWidth + rand.Intn(maxSkew) + 3
+		x += dotSize*numberWidth + rand.Intn(maxSkew) + 8
 	}
+	drawCirclesLine(img, color)
 	return img
 }
 

+ 18 - 2
draw.go

@@ -38,6 +38,7 @@ func drawCircle(img *image.NRGBA, color image.Color, x0, y0, radius int) {
 	}
 }
 
+
 func min3(x, y, z uint8) (o uint8) {
 	o = x
 	if y < o {
@@ -82,6 +83,21 @@ func fillWithCircles(img *image.NRGBA, color image.NRGBAColor, n, maxradius int)
 	}
 }
 
+func drawCirclesLine(img *image.NRGBA, color image.Color) {
+	r := 0
+	maxx := img.Bounds().Max.X
+	maxy := img.Bounds().Max.Y
+	y := rand.Intn(maxy/2+maxy/3) - rand.Intn(maxy/3)
+	for x := 0; x < maxx; x += r  {
+		r = rand.Intn(dotSize/2)
+		y += rand.Intn(3) - 1
+		if y <= 0 || y >= maxy {
+			y = rand.Intn(maxy/2) + rand.Intn(maxy/2)
+		}
+		drawCircle(img, color, x, y, r)
+	}
+}
+
 func drawNumber(img *image.NRGBA, number []byte, x, y int, color image.NRGBAColor) {
 	skf := rand.Intn(maxSkew) - maxSkew/2
 	if skf < 0 {
@@ -90,8 +106,8 @@ func drawNumber(img *image.NRGBA, number []byte, x, y int, color image.NRGBAColo
 	for y0 := 0; y0 < numberHeight; y0++ {
 		for x0 := 0; x0 < numberWidth; x0++ {
 			radius := rand.Intn(dotSize/2) + dotSize/2
-			addx := rand.Intn(radius / 2)
-			addy := rand.Intn(radius / 2)
+			addx := rand.Intn(radius / 4)
+			addy := rand.Intn(radius / 4)
 			if number[y0*numberWidth+x0] == 1 {
 				drawCircle(img, color, x+x0*dotSize+dotSize+addx,
 					y+y0*dotSize+dotSize+addy, radius)

+ 29 - 29
font.go

@@ -6,17 +6,17 @@ const (
 )
 
 var font = [][]byte{
-	{
-		0, 1, 1, 1, 0,
-		1, 0, 0, 0, 1,
-		1, 0, 0, 0, 1,
-		1, 0, 0, 0, 1,
-		1, 0, 0, 0, 1,
-		1, 0, 0, 0, 1,
-		1, 0, 0, 0, 1,
-		0, 1, 1, 1, 0,
+	{ // 0
+		0,1,1,1,0,
+		1,0,0,0,1,
+		1,0,0,0,1,
+		1,0,0,0,1,
+		1,0,0,0,1,
+		1,0,0,0,1,
+		1,0,0,0,1,
+		0,1,1,1,0,
 	},
-	{
+	{ // 1
 		0, 0, 1, 0, 0,
 		0, 1, 1, 0, 0,
 		1, 0, 1, 0, 0,
@@ -26,7 +26,7 @@ var font = [][]byte{
 		0, 0, 1, 0, 0,
 		1, 1, 1, 1, 1,
 	},
-	{
+	{ // 2
 		0, 1, 1, 1, 0,
 		1, 0, 0, 0, 1,
 		0, 0, 0, 0, 1,
@@ -36,17 +36,17 @@ var font = [][]byte{
 		1, 0, 0, 0, 0,
 		1, 1, 1, 1, 1,
 	},
-	{
-		1, 1, 1, 1, 1,
+	{ // 3
+		1, 1, 1, 1, 0,
 		0, 0, 0, 0, 1,
-		0, 0, 0, 1, 1,
-		0, 1, 1, 0, 0,
+		0, 0, 0, 1, 0,
+		0, 1, 1, 1, 0,
 		0, 0, 0, 1, 0,
 		0, 0, 0, 0, 1,
 		0, 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