...

Source file src/image/decode_test.go

Documentation: image

		 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  	{"testdata/video-001.progressive.truncated.png", "testdata/video-001.progressive.truncated.jpeg", 8 << 8},
		40  	// Grayscale images.
		41  	{"testdata/video-005.gray.png", "testdata/video-005.gray.jpeg", 8 << 8},
		42  	{"testdata/video-005.gray.png", "testdata/video-005.gray.png", 0},
		43  }
		44  
		45  func decode(filename string) (image.Image, string, error) {
		46  	f, err := os.Open(filename)
		47  	if err != nil {
		48  		return nil, "", err
		49  	}
		50  	defer f.Close()
		51  	return image.Decode(bufio.NewReader(f))
		52  }
		53  
		54  func decodeConfig(filename string) (image.Config, string, error) {
		55  	f, err := os.Open(filename)
		56  	if err != nil {
		57  		return image.Config{}, "", err
		58  	}
		59  	defer f.Close()
		60  	return image.DecodeConfig(bufio.NewReader(f))
		61  }
		62  
		63  func delta(u0, u1 uint32) int {
		64  	d := int(u0) - int(u1)
		65  	if d < 0 {
		66  		return -d
		67  	}
		68  	return d
		69  }
		70  
		71  func withinTolerance(c0, c1 color.Color, tolerance int) bool {
		72  	r0, g0, b0, a0 := c0.RGBA()
		73  	r1, g1, b1, a1 := c1.RGBA()
		74  	r := delta(r0, r1)
		75  	g := delta(g0, g1)
		76  	b := delta(b0, b1)
		77  	a := delta(a0, a1)
		78  	return r <= tolerance && g <= tolerance && b <= tolerance && a <= tolerance
		79  }
		80  
		81  func TestDecode(t *testing.T) {
		82  	rgba := func(c color.Color) string {
		83  		r, g, b, a := c.RGBA()
		84  		return fmt.Sprintf("rgba = 0x%04x, 0x%04x, 0x%04x, 0x%04x for %T%v", r, g, b, a, c, c)
		85  	}
		86  
		87  	golden := make(map[string]image.Image)
		88  loop:
		89  	for _, it := range imageTests {
		90  		g := golden[it.goldenFilename]
		91  		if g == nil {
		92  			var err error
		93  			g, _, err = decode(it.goldenFilename)
		94  			if err != nil {
		95  				t.Errorf("%s: %v", it.goldenFilename, err)
		96  				continue loop
		97  			}
		98  			golden[it.goldenFilename] = g
		99  		}
	 100  		m, imageFormat, err := decode(it.filename)
	 101  		if err != nil {
	 102  			t.Errorf("%s: %v", it.filename, err)
	 103  			continue loop
	 104  		}
	 105  		b := g.Bounds()
	 106  		if !b.Eq(m.Bounds()) {
	 107  			t.Errorf("%s: got bounds %v want %v", it.filename, m.Bounds(), b)
	 108  			continue loop
	 109  		}
	 110  		for y := b.Min.Y; y < b.Max.Y; y++ {
	 111  			for x := b.Min.X; x < b.Max.X; x++ {
	 112  				if !withinTolerance(g.At(x, y), m.At(x, y), it.tolerance) {
	 113  					t.Errorf("%s: at (%d, %d):\ngot	%v\nwant %v",
	 114  						it.filename, x, y, rgba(m.At(x, y)), rgba(g.At(x, y)))
	 115  					continue loop
	 116  				}
	 117  			}
	 118  		}
	 119  		if imageFormat == "gif" {
	 120  			// Each frame of a GIF can have a frame-local palette override the
	 121  			// GIF-global palette. Thus, image.Decode can yield a different ColorModel
	 122  			// than image.DecodeConfig.
	 123  			continue
	 124  		}
	 125  		c, _, err := decodeConfig(it.filename)
	 126  		if err != nil {
	 127  			t.Errorf("%s: %v", it.filename, err)
	 128  			continue loop
	 129  		}
	 130  		if m.ColorModel() != c.ColorModel {
	 131  			t.Errorf("%s: color models differ", it.filename)
	 132  			continue loop
	 133  		}
	 134  	}
	 135  }
	 136  

View as plain text