diff options
Diffstat (limited to 'src/pkg/image/decode_test.go')
| -rw-r--r-- | src/pkg/image/decode_test.go | 70 | 
1 files changed, 52 insertions, 18 deletions
| diff --git a/src/pkg/image/decode_test.go b/src/pkg/image/decode_test.go index 0716ad905..540d5eda5 100644 --- a/src/pkg/image/decode_test.go +++ b/src/pkg/image/decode_test.go @@ -10,27 +10,34 @@ import (  	"os"  	"testing" -	// TODO(nigeltao): implement bmp, gif and tiff decoders. +	_ "image/bmp" +	_ "image/gif"  	_ "image/jpeg"  	_ "image/png" +	_ "image/tiff"  ) -const goldenFile = "testdata/video-001.png" -  type imageTest struct { -	filename  string -	tolerance int +	goldenFilename string +	filename       string +	tolerance      int  }  var imageTests = []imageTest{ -	//{"testdata/video-001.bmp", 0}, +	{"testdata/video-001.png", "testdata/video-001.bmp", 0},  	// GIF images are restricted to a 256-color palette and the conversion  	// to GIF loses significant image quality. -	//{"testdata/video-001.gif", 64<<8}, +	{"testdata/video-001.png", "testdata/video-001.gif", 64 << 8}, +	{"testdata/video-001.png", "testdata/video-001.interlaced.gif", 64 << 8}, +	{"testdata/video-001.png", "testdata/video-001.5bpp.gif", 128 << 8},  	// JPEG is a lossy format and hence needs a non-zero tolerance. -	{"testdata/video-001.jpeg", 8 << 8}, -	{"testdata/video-001.png", 0}, -	//{"testdata/video-001.tiff", 0}, +	{"testdata/video-001.png", "testdata/video-001.jpeg", 8 << 8}, +	{"testdata/video-001.png", "testdata/video-001.png", 0}, +	{"testdata/video-001.png", "testdata/video-001.tiff", 0}, + +	// Test grayscale images. +	{"testdata/video-005.gray.png", "testdata/video-005.gray.jpeg", 8 << 8}, +	{"testdata/video-005.gray.png", "testdata/video-005.gray.png", 0},  }  func decode(filename string) (image.Image, string, os.Error) { @@ -42,6 +49,15 @@ func decode(filename string) (image.Image, string, os.Error) {  	return image.Decode(bufio.NewReader(f))  } +func decodeConfig(filename string) (image.Config, string, os.Error) { +	f, err := os.Open(filename) +	if err != nil { +		return image.Config{}, "", err +	} +	defer f.Close() +	return image.DecodeConfig(bufio.NewReader(f)) +} +  func delta(u0, u1 uint32) int {  	d := int(u0) - int(u1)  	if d < 0 { @@ -61,29 +77,47 @@ func withinTolerance(c0, c1 image.Color, tolerance int) bool {  }  func TestDecode(t *testing.T) { -	golden, _, err := decode(goldenFile) -	if err != nil { -		t.Errorf("%s: %v", goldenFile, err) -	} +	golden := make(map[string]image.Image)  loop:  	for _, it := range imageTests { -		m, _, err := decode(it.filename) +		g := golden[it.goldenFilename] +		if g == nil { +			var err os.Error +			g, _, err = decode(it.goldenFilename) +			if err != nil { +				t.Errorf("%s: %v", it.goldenFilename, err) +				continue loop +			} +			golden[it.goldenFilename] = g +		} +		m, imageFormat, err := decode(it.filename)  		if err != nil {  			t.Errorf("%s: %v", it.filename, err)  			continue loop  		} -		b := golden.Bounds() +		b := g.Bounds()  		if !b.Eq(m.Bounds()) {  			t.Errorf("%s: want bounds %v got %v", it.filename, b, m.Bounds())  			continue loop  		}  		for y := b.Min.Y; y < b.Max.Y; y++ {  			for x := b.Min.X; x < b.Max.X; x++ { -				if !withinTolerance(golden.At(x, y), m.At(x, y), it.tolerance) { -					t.Errorf("%s: at (%d, %d), want %v got %v", it.filename, x, y, golden.At(x, y), m.At(x, y)) +				if !withinTolerance(g.At(x, y), m.At(x, y), it.tolerance) { +					t.Errorf("%s: at (%d, %d), want %v got %v", it.filename, x, y, g.At(x, y), m.At(x, y))  					continue loop  				}  			}  		} +		if imageFormat == "gif" { +			// Each frame of a GIF can have a frame-local palette override the +			// GIF-global palette. Thus, image.Decode can yield a different ColorModel +			// than image.DecodeConfig. +			continue +		} +		c, _, err := decodeConfig(it.filename) +		if m.ColorModel() != c.ColorModel { +			t.Errorf("%s: color models differ", it.filename) +			continue loop +		}  	}  } | 
