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.
19 type imageTest
struct {
25 var imageTests
= []imageTest
{
26 {"testdata/video-001.png", "testdata/video-001.png", 0},
27 // GIF images are restricted to a 256-color palette and the conversion
28 // to GIF loses significant image quality.
29 {"testdata/video-001.png", "testdata/video-001.gif", 64 << 8},
30 {"testdata/video-001.png", "testdata/video-001.interlaced.gif", 64 << 8},
31 {"testdata/video-001.png", "testdata/video-001.5bpp.gif", 128 << 8},
32 // JPEG is a lossy format and hence needs a non-zero tolerance.
33 {"testdata/video-001.png", "testdata/video-001.jpeg", 8 << 8},
34 {"testdata/video-001.png", "testdata/video-001.progressive.jpeg", 8 << 8},
36 {"testdata/video-005.gray.png", "testdata/video-005.gray.jpeg", 8 << 8},
37 {"testdata/video-005.gray.png", "testdata/video-005.gray.png", 0},
40 func decode(filename
string) (image
.Image
, string, error
) {
41 f
, err
:= os
.Open(filename
)
46 return image
.Decode(bufio
.NewReader(f
))
49 func decodeConfig(filename
string) (image
.Config
, string, error
) {
50 f
, err
:= os
.Open(filename
)
52 return image
.Config
{}, "", err
55 return image
.DecodeConfig(bufio
.NewReader(f
))
58 func delta(u0
, u1
uint32) int {
59 d
:= int(u0
) - int(u1
)
66 func withinTolerance(c0
, c1 color
.Color
, tolerance
int) bool {
67 r0
, g0
, b0
, a0
:= c0
.RGBA()
68 r1
, g1
, b1
, a1
:= c1
.RGBA()
73 return r
<= tolerance
&& g
<= tolerance
&& b
<= tolerance
&& a
<= tolerance
76 func TestDecode(t
*testing
.T
) {
77 golden
:= make(map[string]image
.Image
)
79 for _
, it
:= range imageTests
{
80 g
:= golden
[it
.goldenFilename
]
83 g
, _
, err
= decode(it
.goldenFilename
)
85 t
.Errorf("%s: %v", it
.goldenFilename
, err
)
88 golden
[it
.goldenFilename
] = g
90 m
, imageFormat
, err
:= decode(it
.filename
)
92 t
.Errorf("%s: %v", it
.filename
, err
)
96 if !b
.Eq(m
.Bounds()) {
97 t
.Errorf("%s: want bounds %v got %v", it
.filename
, b
, m
.Bounds())
100 for y
:= b
.Min
.Y
; y
< b
.Max
.Y
; y
++ {
101 for x
:= b
.Min
.X
; x
< b
.Max
.X
; x
++ {
102 if !withinTolerance(g
.At(x
, y
), m
.At(x
, y
), it
.tolerance
) {
103 t
.Errorf("%s: at (%d, %d), want %v got %v", it
.filename
, x
, y
, g
.At(x
, y
), m
.At(x
, y
))
108 if imageFormat
== "gif" {
109 // Each frame of a GIF can have a frame-local palette override the
110 // GIF-global palette. Thus, image.Decode can yield a different ColorModel
111 // than image.DecodeConfig.
114 c
, _
, err
:= decodeConfig(it
.filename
)
115 if m
.ColorModel() != c
.ColorModel
{
116 t
.Errorf("%s: color models differ", it
.filename
)