OLD | NEW |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package image_test | 5 package image_test |
6 | 6 |
7 import ( | 7 import ( |
8 "bufio" | 8 "bufio" |
9 "image" | 9 "image" |
10 "image/color" | 10 "image/color" |
11 "os" | 11 "os" |
12 "testing" | 12 "testing" |
13 | 13 |
14 _ "image/gif" | 14 _ "image/gif" |
15 _ "image/jpeg" | 15 _ "image/jpeg" |
16 _ "image/png" | 16 _ "image/png" |
17 ) | 17 ) |
18 | 18 |
19 type imageTest struct { | 19 type imageTest struct { |
20 goldenFilename string | 20 goldenFilename string |
21 filename string | 21 filename string |
22 tolerance int | 22 tolerance int |
23 } | 23 } |
24 | 24 |
25 var imageTests = []imageTest{ | 25 var imageTests = []imageTest{ |
26 {"testdata/video-001.png", "testdata/video-001.png", 0}, | 26 {"testdata/video-001.png", "testdata/video-001.png", 0}, |
| 27 |
27 // GIF images are restricted to a 256-color palette and the conversion | 28 // GIF images are restricted to a 256-color palette and the conversion |
28 // to GIF loses significant image quality. | 29 // 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.gif", 64 << 8}, |
30 {"testdata/video-001.png", "testdata/video-001.interlaced.gif", 64 << 8}
, | 31 {"testdata/video-001.png", "testdata/video-001.interlaced.gif", 64 << 8}
, |
31 {"testdata/video-001.png", "testdata/video-001.5bpp.gif", 128 << 8}, | 32 {"testdata/video-001.png", "testdata/video-001.5bpp.gif", 128 << 8}, |
| 33 // Images with invalid bounds should not cause huge allocations. |
| 34 // This image from https://bugzilla.mozilla.org/show_bug.cgi?id=525326 |
| 35 {"", "testdata/issue5050.gif", 64 << 8}, |
| 36 |
32 // JPEG is a lossy format and hence needs a non-zero tolerance. | 37 // JPEG is a lossy format and hence needs a non-zero tolerance. |
33 {"testdata/video-001.png", "testdata/video-001.jpeg", 8 << 8}, | 38 {"testdata/video-001.png", "testdata/video-001.jpeg", 8 << 8}, |
34 {"testdata/video-001.png", "testdata/video-001.progressive.jpeg", 8 << 8
}, | 39 {"testdata/video-001.png", "testdata/video-001.progressive.jpeg", 8 << 8
}, |
35 // Grayscale images. | 40 // Grayscale images. |
36 {"testdata/video-005.gray.png", "testdata/video-005.gray.jpeg", 8 << 8}, | 41 {"testdata/video-005.gray.png", "testdata/video-005.gray.jpeg", 8 << 8}, |
37 {"testdata/video-005.gray.png", "testdata/video-005.gray.png", 0}, | 42 {"testdata/video-005.gray.png", "testdata/video-005.gray.png", 0}, |
38 } | 43 } |
39 | 44 |
40 func decode(filename string) (image.Image, string, error) { | 45 func decode(filename string) (image.Image, string, error) { |
41 f, err := os.Open(filename) | 46 f, err := os.Open(filename) |
(...skipping 29 matching lines...) Expand all Loading... |
71 b := delta(b0, b1) | 76 b := delta(b0, b1) |
72 a := delta(a0, a1) | 77 a := delta(a0, a1) |
73 return r <= tolerance && g <= tolerance && b <= tolerance && a <= tolera
nce | 78 return r <= tolerance && g <= tolerance && b <= tolerance && a <= tolera
nce |
74 } | 79 } |
75 | 80 |
76 func TestDecode(t *testing.T) { | 81 func TestDecode(t *testing.T) { |
77 golden := make(map[string]image.Image) | 82 golden := make(map[string]image.Image) |
78 loop: | 83 loop: |
79 for _, it := range imageTests { | 84 for _, it := range imageTests { |
80 g := golden[it.goldenFilename] | 85 g := golden[it.goldenFilename] |
81 » » if g == nil { | 86 » » if g == nil && it.goldenFilename != "" { |
82 var err error | 87 var err error |
83 g, _, err = decode(it.goldenFilename) | 88 g, _, err = decode(it.goldenFilename) |
84 if err != nil { | 89 if err != nil { |
85 t.Errorf("%s: %v", it.goldenFilename, err) | 90 t.Errorf("%s: %v", it.goldenFilename, err) |
86 continue loop | 91 continue loop |
87 } | 92 } |
88 golden[it.goldenFilename] = g | 93 golden[it.goldenFilename] = g |
89 } | 94 } |
90 m, imageFormat, err := decode(it.filename) | 95 m, imageFormat, err := decode(it.filename) |
91 » » if err != nil { | 96 » » if g == nil { |
92 » » » t.Errorf("%s: %v", it.filename, err) | 97 » » » if err == nil { |
| 98 » » » » t.Errorf("%s: expected error, got success", it.f
ilename) |
| 99 » » » } |
93 continue loop | 100 continue loop |
| 101 } else { |
| 102 if err != nil { |
| 103 t.Errorf("%s: %v", it.filename, err) |
| 104 continue loop |
| 105 } |
94 } | 106 } |
95 b := g.Bounds() | 107 b := g.Bounds() |
96 if !b.Eq(m.Bounds()) { | 108 if !b.Eq(m.Bounds()) { |
97 t.Errorf("%s: want bounds %v got %v", it.filename, b, m.
Bounds()) | 109 t.Errorf("%s: want bounds %v got %v", it.filename, b, m.
Bounds()) |
98 continue loop | 110 continue loop |
99 } | 111 } |
100 for y := b.Min.Y; y < b.Max.Y; y++ { | 112 for y := b.Min.Y; y < b.Max.Y; y++ { |
101 for x := b.Min.X; x < b.Max.X; x++ { | 113 for x := b.Min.X; x < b.Max.X; x++ { |
102 if !withinTolerance(g.At(x, y), m.At(x, y), it.t
olerance) { | 114 if !withinTolerance(g.At(x, y), m.At(x, y), it.t
olerance) { |
103 t.Errorf("%s: at (%d, %d), want %v got %
v", it.filename, x, y, g.At(x, y), m.At(x, y)) | 115 t.Errorf("%s: at (%d, %d), want %v got %
v", it.filename, x, y, g.At(x, y), m.At(x, y)) |
104 continue loop | 116 continue loop |
105 } | 117 } |
106 } | 118 } |
107 } | 119 } |
108 if imageFormat == "gif" { | 120 if imageFormat == "gif" { |
109 // Each frame of a GIF can have a frame-local palette ov
erride the | 121 // Each frame of a GIF can have a frame-local palette ov
erride the |
110 // GIF-global palette. Thus, image.Decode can yield a di
fferent ColorModel | 122 // GIF-global palette. Thus, image.Decode can yield a di
fferent ColorModel |
111 // than image.DecodeConfig. | 123 // than image.DecodeConfig. |
112 continue | 124 continue |
113 } | 125 } |
114 c, _, err := decodeConfig(it.filename) | 126 c, _, err := decodeConfig(it.filename) |
115 if m.ColorModel() != c.ColorModel { | 127 if m.ColorModel() != c.ColorModel { |
116 t.Errorf("%s: color models differ", it.filename) | 128 t.Errorf("%s: color models differ", it.filename) |
117 continue loop | 129 continue loop |
118 } | 130 } |
119 } | 131 } |
120 } | 132 } |
OLD | NEW |