Browse Source

Replace PNGEncode with WriteTo for Image.

Dmitry Chestnykh 14 years ago
parent
commit
797447596c
3 changed files with 9 additions and 5 deletions
  1. 2 1
      captcha.go
  2. 1 1
      cmd/image/main.go
  3. 6 3
      image.go

+ 2 - 1
captcha.go

@@ -54,7 +54,8 @@ func WriteImage(w io.Writer, id string, width, height int) os.Error {
 	if ns == nil {
 		return os.NewError("captcha id not found")
 	}
-	return NewImage(ns, width, height).PNGEncode(w)
+	_, err := NewImage(ns, width, height).WriteTo(w)
+	return err
 }
 
 // WriteAudio writes WAV-encoded audio captcha with the given captcha id into

+ 1 - 1
cmd/image/main.go

@@ -7,5 +7,5 @@ import (
 
 func main() {
 	img, _ := captcha.NewRandomImage(captcha.StdLength, captcha.StdWidth, captcha.StdHeight)
-	img.PNGEncode(os.Stdout)
+	img.WriteTo(os.Stdout)
 }

+ 6 - 3
image.go

@@ -68,9 +68,12 @@ func NewRandomImage(length, width, height int) (img *Image, numbers []byte) {
 	return
 }
 
-// PNGEncode writes captcha image in PNG format into the given writer.
-func (img *Image) PNGEncode(w io.Writer) os.Error {
-	return png.Encode(w, img)
+// WriteTo writes captcha image in PNG format into the given writer.
+//
+// Bug: while Image conforms to io.WriterTo interface, this function returns 0
+// instead of the actual bytes written because png.Encode doesn't report this.
+func (img *Image) WriteTo(w io.Writer) (int64, os.Error) {
+	return 0, png.Encode(w, img)
 }
 
 func (img *Image) calculateSizes(width, height, ncount int) {