OSDN Git Service

Update Go library to last weekly.
[pf3gnuchains/gcc-fork.git] / libgo / go / image / decode_test.go
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.
4
5 package image_test
6
7 import (
8         "bufio"
9         "image"
10         "image/color"
11         "os"
12         "testing"
13
14         _ "image/bmp"
15         _ "image/gif"
16         _ "image/jpeg"
17         _ "image/png"
18         _ "image/tiff"
19 )
20
21 type imageTest struct {
22         goldenFilename string
23         filename       string
24         tolerance      int
25 }
26
27 var imageTests = []imageTest{
28         {"testdata/video-001.png", "testdata/video-001.bmp", 0},
29         // GIF images are restricted to a 256-color palette and the conversion
30         // to GIF loses significant image quality.
31         {"testdata/video-001.png", "testdata/video-001.gif", 64 << 8},
32         {"testdata/video-001.png", "testdata/video-001.interlaced.gif", 64 << 8},
33         {"testdata/video-001.png", "testdata/video-001.5bpp.gif", 128 << 8},
34         // JPEG is a lossy format and hence needs a non-zero tolerance.
35         {"testdata/video-001.png", "testdata/video-001.jpeg", 8 << 8},
36         {"testdata/video-001.png", "testdata/video-001.png", 0},
37         {"testdata/video-001.png", "testdata/video-001.tiff", 0},
38
39         // Test grayscale images.
40         {"testdata/video-005.gray.png", "testdata/video-005.gray.jpeg", 8 << 8},
41         {"testdata/video-005.gray.png", "testdata/video-005.gray.png", 0},
42 }
43
44 func decode(filename string) (image.Image, string, os.Error) {
45         f, err := os.Open(filename)
46         if err != nil {
47                 return nil, "", err
48         }
49         defer f.Close()
50         return image.Decode(bufio.NewReader(f))
51 }
52
53 func decodeConfig(filename string) (image.Config, string, os.Error) {
54         f, err := os.Open(filename)
55         if err != nil {
56                 return image.Config{}, "", err
57         }
58         defer f.Close()
59         return image.DecodeConfig(bufio.NewReader(f))
60 }
61
62 func delta(u0, u1 uint32) int {
63         d := int(u0) - int(u1)
64         if d < 0 {
65                 return -d
66         }
67         return d
68 }
69
70 func withinTolerance(c0, c1 color.Color, tolerance int) bool {
71         r0, g0, b0, a0 := c0.RGBA()
72         r1, g1, b1, a1 := c1.RGBA()
73         r := delta(r0, r1)
74         g := delta(g0, g1)
75         b := delta(b0, b1)
76         a := delta(a0, a1)
77         return r <= tolerance && g <= tolerance && b <= tolerance && a <= tolerance
78 }
79
80 func TestDecode(t *testing.T) {
81         golden := make(map[string]image.Image)
82 loop:
83         for _, it := range imageTests {
84                 g := golden[it.goldenFilename]
85                 if g == nil {
86                         var err os.Error
87                         g, _, err = decode(it.goldenFilename)
88                         if err != nil {
89                                 t.Errorf("%s: %v", it.goldenFilename, err)
90                                 continue loop
91                         }
92                         golden[it.goldenFilename] = g
93                 }
94                 m, imageFormat, err := decode(it.filename)
95                 if err != nil {
96                         t.Errorf("%s: %v", it.filename, err)
97                         continue loop
98                 }
99                 b := g.Bounds()
100                 if !b.Eq(m.Bounds()) {
101                         t.Errorf("%s: want bounds %v got %v", it.filename, b, m.Bounds())
102                         continue loop
103                 }
104                 for y := b.Min.Y; y < b.Max.Y; y++ {
105                         for x := b.Min.X; x < b.Max.X; x++ {
106                                 if !withinTolerance(g.At(x, y), m.At(x, y), it.tolerance) {
107                                         t.Errorf("%s: at (%d, %d), want %v got %v", it.filename, x, y, g.At(x, y), m.At(x, y))
108                                         continue loop
109                                 }
110                         }
111                 }
112                 if imageFormat == "gif" {
113                         // Each frame of a GIF can have a frame-local palette override the
114                         // GIF-global palette. Thus, image.Decode can yield a different ColorModel
115                         // than image.DecodeConfig.
116                         continue
117                 }
118                 c, _, err := decodeConfig(it.filename)
119                 if m.ColorModel() != c.ColorModel {
120                         t.Errorf("%s: color models differ", it.filename)
121                         continue loop
122                 }
123         }
124 }