OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [image/] [decode_test.go] - Blame information for rev 867

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.