github.com/ccccaoqing/test@v0.0.0-20220510085219-3985d23445c0/src/image/decode_test.go (about) 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/gif" 15 _ "image/jpeg" 16 _ "image/png" 17 ) 18 19 type imageTest struct { 20 goldenFilename string 21 filename string 22 tolerance int 23 } 24 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}, 35 // Grayscale images. 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}, 38 } 39 40 func decode(filename string) (image.Image, string, error) { 41 f, err := os.Open(filename) 42 if err != nil { 43 return nil, "", err 44 } 45 defer f.Close() 46 return image.Decode(bufio.NewReader(f)) 47 } 48 49 func decodeConfig(filename string) (image.Config, string, error) { 50 f, err := os.Open(filename) 51 if err != nil { 52 return image.Config{}, "", err 53 } 54 defer f.Close() 55 return image.DecodeConfig(bufio.NewReader(f)) 56 } 57 58 func delta(u0, u1 uint32) int { 59 d := int(u0) - int(u1) 60 if d < 0 { 61 return -d 62 } 63 return d 64 } 65 66 func withinTolerance(c0, c1 color.Color, tolerance int) bool { 67 r0, g0, b0, a0 := c0.RGBA() 68 r1, g1, b1, a1 := c1.RGBA() 69 r := delta(r0, r1) 70 g := delta(g0, g1) 71 b := delta(b0, b1) 72 a := delta(a0, a1) 73 return r <= tolerance && g <= tolerance && b <= tolerance && a <= tolerance 74 } 75 76 func TestDecode(t *testing.T) { 77 golden := make(map[string]image.Image) 78 loop: 79 for _, it := range imageTests { 80 g := golden[it.goldenFilename] 81 if g == nil { 82 var err error 83 g, _, err = decode(it.goldenFilename) 84 if err != nil { 85 t.Errorf("%s: %v", it.goldenFilename, err) 86 continue loop 87 } 88 golden[it.goldenFilename] = g 89 } 90 m, imageFormat, err := decode(it.filename) 91 if err != nil { 92 t.Errorf("%s: %v", it.filename, err) 93 continue loop 94 } 95 b := g.Bounds() 96 if !b.Eq(m.Bounds()) { 97 t.Errorf("%s: want bounds %v got %v", it.filename, b, m.Bounds()) 98 continue loop 99 } 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)) 104 continue loop 105 } 106 } 107 } 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. 112 continue 113 } 114 c, _, err := decodeConfig(it.filename) 115 if m.ColorModel() != c.ColorModel { 116 t.Errorf("%s: color models differ", it.filename) 117 continue loop 118 } 119 } 120 }