github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/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  	"fmt"
    10  	"image"
    11  	"image/color"
    12  	"os"
    13  	"testing"
    14  
    15  	_ "image/gif"
    16  	_ "image/jpeg"
    17  	_ "image/png"
    18  )
    19  
    20  type imageTest struct {
    21  	goldenFilename string
    22  	filename       string
    23  	tolerance      int
    24  }
    25  
    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  	// 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, 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, 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  	rgba := func(c color.Color) string {
    82  		r, g, b, a := c.RGBA()
    83  		return fmt.Sprintf("rgba = 0x%04x, 0x%04x, 0x%04x, 0x%04x for %T%v", r, g, b, a, c, c)
    84  	}
    85  
    86  	golden := make(map[string]image.Image)
    87  loop:
    88  	for _, it := range imageTests {
    89  		g := golden[it.goldenFilename]
    90  		if g == nil {
    91  			var err error
    92  			g, _, err = decode(it.goldenFilename)
    93  			if err != nil {
    94  				t.Errorf("%s: %v", it.goldenFilename, err)
    95  				continue loop
    96  			}
    97  			golden[it.goldenFilename] = g
    98  		}
    99  		m, imageFormat, err := decode(it.filename)
   100  		if err != nil {
   101  			t.Errorf("%s: %v", it.filename, err)
   102  			continue loop
   103  		}
   104  		b := g.Bounds()
   105  		if !b.Eq(m.Bounds()) {
   106  			t.Errorf("%s: got bounds %v want %v", it.filename, m.Bounds(), b)
   107  			continue loop
   108  		}
   109  		for y := b.Min.Y; y < b.Max.Y; y++ {
   110  			for x := b.Min.X; x < b.Max.X; x++ {
   111  				if !withinTolerance(g.At(x, y), m.At(x, y), it.tolerance) {
   112  					t.Errorf("%s: at (%d, %d):\ngot  %v\nwant %v",
   113  						it.filename, x, y, rgba(m.At(x, y)), rgba(g.At(x, y)))
   114  					continue loop
   115  				}
   116  			}
   117  		}
   118  		if imageFormat == "gif" {
   119  			// Each frame of a GIF can have a frame-local palette override the
   120  			// GIF-global palette. Thus, image.Decode can yield a different ColorModel
   121  			// than image.DecodeConfig.
   122  			continue
   123  		}
   124  		c, _, err := decodeConfig(it.filename)
   125  		if m.ColorModel() != c.ColorModel {
   126  			t.Errorf("%s: color models differ", it.filename)
   127  			continue loop
   128  		}
   129  	}
   130  }