]> Cypherpunks repositories - gostls13.git/commitdiff
image: add a decoding test for common file formats.
authorNigel Tao <nigeltao@golang.org>
Thu, 3 Mar 2011 09:35:49 +0000 (20:35 +1100)
committerNigel Tao <nigeltao@golang.org>
Thu, 3 Mar 2011 09:35:49 +0000 (20:35 +1100)
The test image was converted from doc/video-001.png using the
convert command line tool (ImageMagick 6.5.7-8) at -quality 100.

R=r, nigeltao_gnome
CC=golang-dev
https://golang.org/cl/4259047

src/pkg/Makefile
src/pkg/image/decode_test.go [new file with mode: 0644]
src/pkg/image/testdata/video-001.bmp [new file with mode: 0644]
src/pkg/image/testdata/video-001.gif [new file with mode: 0644]
src/pkg/image/testdata/video-001.jpeg [new file with mode: 0644]
src/pkg/image/testdata/video-001.png [new file with mode: 0644]
src/pkg/image/testdata/video-001.tiff [new file with mode: 0644]

index aaf8ca62e2098548c246aed23922531ff512c0dd..139cee1a48f9c90edeaf44ff70b6a3bb6022cb1a 100644 (file)
@@ -172,7 +172,6 @@ NOTEST=\
        go/token\
        hash\
        http/pprof\
-       image\
        image/jpeg\
        net/dict\
        rand\
diff --git a/src/pkg/image/decode_test.go b/src/pkg/image/decode_test.go
new file mode 100644 (file)
index 0000000..5b87344
--- /dev/null
@@ -0,0 +1,89 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package image_test
+
+import (
+       "bufio"
+       "image"
+       "os"
+       "testing"
+
+       // TODO(nigeltao): implement bmp, gif and tiff decoders.
+       _ "image/jpeg"
+       _ "image/png"
+)
+
+const goldenFile = "testdata/video-001.png"
+
+type imageTest struct {
+       filename  string
+       tolerance int
+}
+
+var imageTests = []imageTest{
+       //{"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},
+       // 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},
+}
+
+func decode(filename string) (image.Image, string, os.Error) {
+       f, err := os.Open(filename, os.O_RDONLY, 0400)
+       if err != nil {
+               return nil, "", err
+       }
+       defer f.Close()
+       return image.Decode(bufio.NewReader(f))
+}
+
+func delta(u0, u1 uint32) int {
+       d := int(u0) - int(u1)
+       if d < 0 {
+               return -d
+       }
+       return d
+}
+
+func withinTolerance(c0, c1 image.Color, tolerance int) bool {
+       r0, g0, b0, a0 := c0.RGBA()
+       r1, g1, b1, a1 := c1.RGBA()
+       r := delta(r0, r1)
+       g := delta(g0, g1)
+       b := delta(b0, b1)
+       a := delta(a0, a1)
+       return r <= tolerance && g <= tolerance && b <= tolerance && a <= tolerance
+}
+
+func TestDecode(t *testing.T) {
+       golden, _, err := decode(goldenFile)
+       if err != nil {
+               t.Errorf("%s: %v", goldenFile, err)
+       }
+loop:
+       for _, it := range imageTests {
+               m, _, err := decode(it.filename)
+               if err != nil {
+                       t.Errorf("%s: %v", it.filename, err)
+                       continue loop
+               }
+               b := golden.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))
+                                       continue loop
+                               }
+                       }
+               }
+       }
+}
diff --git a/src/pkg/image/testdata/video-001.bmp b/src/pkg/image/testdata/video-001.bmp
new file mode 100644 (file)
index 0000000..ca3dd42
Binary files /dev/null and b/src/pkg/image/testdata/video-001.bmp differ
diff --git a/src/pkg/image/testdata/video-001.gif b/src/pkg/image/testdata/video-001.gif
new file mode 100644 (file)
index 0000000..ca06af6
Binary files /dev/null and b/src/pkg/image/testdata/video-001.gif differ
diff --git a/src/pkg/image/testdata/video-001.jpeg b/src/pkg/image/testdata/video-001.jpeg
new file mode 100644 (file)
index 0000000..1b87c93
Binary files /dev/null and b/src/pkg/image/testdata/video-001.jpeg differ
diff --git a/src/pkg/image/testdata/video-001.png b/src/pkg/image/testdata/video-001.png
new file mode 100644 (file)
index 0000000..d3468bb
Binary files /dev/null and b/src/pkg/image/testdata/video-001.png differ
diff --git a/src/pkg/image/testdata/video-001.tiff b/src/pkg/image/testdata/video-001.tiff
new file mode 100644 (file)
index 0000000..0dd6cd9
Binary files /dev/null and b/src/pkg/image/testdata/video-001.tiff differ