1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
20 type imageTest struct {
26 var imageTests = []imageTest{
27 {"testdata/video-001.png", "testdata/video-001.png", 0},
28 // GIF images are restricted to a 256-color palette and the conversion
29 // to GIF loses significant image quality.
30 {"testdata/video-001.png", "testdata/video-001.gif", 64 << 8},
31 {"testdata/video-001.png", "testdata/video-001.interlaced.gif", 64 << 8},
32 {"testdata/video-001.png", "testdata/video-001.5bpp.gif", 128 << 8},
33 // JPEG is a lossy format and hence needs a non-zero tolerance.
34 {"testdata/video-001.png", "testdata/video-001.jpeg", 8 << 8},
35 {"testdata/video-001.png", "testdata/video-001.progressive.jpeg", 8 << 8},
36 {"testdata/video-001.221212.png", "testdata/video-001.221212.jpeg", 8 << 8},
37 {"testdata/video-001.cmyk.png", "testdata/video-001.cmyk.jpeg", 8 << 8},
38 {"testdata/video-001.rgb.png", "testdata/video-001.rgb.jpeg", 8 << 8},
39 {"testdata/video-001.progressive.truncated.png", "testdata/video-001.progressive.truncated.jpeg", 8 << 8},
41 {"testdata/video-005.gray.png", "testdata/video-005.gray.jpeg", 8 << 8},
42 {"testdata/video-005.gray.png", "testdata/video-005.gray.png", 0},
45 func decode(filename string) (image.Image, string, error) {
46 f, err := os.Open(filename)
51 return image.Decode(bufio.NewReader(f))
54 func decodeConfig(filename string) (image.Config, string, error) {
55 f, err := os.Open(filename)
57 return image.Config{}, "", err
60 return image.DecodeConfig(bufio.NewReader(f))
63 func delta(u0, u1 uint32) int {
64 d := int(u0) - int(u1)
71 func withinTolerance(c0, c1 color.Color, tolerance int) bool {
72 r0, g0, b0, a0 := c0.RGBA()
73 r1, g1, b1, a1 := c1.RGBA()
78 return r <= tolerance && g <= tolerance && b <= tolerance && a <= tolerance
81 func TestDecode(t *testing.T) {
82 rgba := func(c color.Color) string {
83 r, g, b, a := c.RGBA()
84 return fmt.Sprintf("rgba = 0x%04x, 0x%04x, 0x%04x, 0x%04x for %T%v", r, g, b, a, c, c)
87 golden := make(map[string]image.Image)
89 for _, it := range imageTests {
90 g := golden[it.goldenFilename]
93 g, _, err = decode(it.goldenFilename)
95 t.Errorf("%s: %v", it.goldenFilename, err)
98 golden[it.goldenFilename] = g
100 m, imageFormat, err := decode(it.filename)
102 t.Errorf("%s: %v", it.filename, err)
106 if !b.Eq(m.Bounds()) {
107 t.Errorf("%s: got bounds %v want %v", it.filename, m.Bounds(), b)
110 for y := b.Min.Y; y < b.Max.Y; y++ {
111 for x := b.Min.X; x < b.Max.X; x++ {
112 if !withinTolerance(g.At(x, y), m.At(x, y), it.tolerance) {
113 t.Errorf("%s: at (%d, %d):\ngot %v\nwant %v",
114 it.filename, x, y, rgba(m.At(x, y)), rgba(g.At(x, y)))
119 if imageFormat == "gif" {
120 // Each frame of a GIF can have a frame-local palette override the
121 // GIF-global palette. Thus, image.Decode can yield a different ColorModel
122 // than image.DecodeConfig.
125 c, _, err := decodeConfig(it.filename)
127 t.Errorf("%s: %v", it.filename, err)
130 if m.ColorModel() != c.ColorModel {
131 t.Errorf("%s: color models differ", it.filename)