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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [image/] [jpeg/] [writer_test.go] - Blame information for rev 848

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 jpeg
6
 
7
import (
8
        "bytes"
9
        "image"
10
        "image/color"
11
        "image/png"
12
        "io/ioutil"
13
        "math/rand"
14
        "os"
15
        "testing"
16
)
17
 
18
var testCase = []struct {
19
        filename  string
20
        quality   int
21
        tolerance int64
22
}{
23
        {"../testdata/video-001.png", 1, 24 << 8},
24
        {"../testdata/video-001.png", 20, 12 << 8},
25
        {"../testdata/video-001.png", 60, 8 << 8},
26
        {"../testdata/video-001.png", 80, 6 << 8},
27
        {"../testdata/video-001.png", 90, 4 << 8},
28
        {"../testdata/video-001.png", 100, 2 << 8},
29
}
30
 
31
func delta(u0, u1 uint32) int64 {
32
        d := int64(u0) - int64(u1)
33
        if d < 0 {
34
                return -d
35
        }
36
        return d
37
}
38
 
39
func readPng(filename string) (image.Image, error) {
40
        f, err := os.Open(filename)
41
        if err != nil {
42
                return nil, err
43
        }
44
        defer f.Close()
45
        return png.Decode(f)
46
}
47
 
48
func TestWriter(t *testing.T) {
49
        for _, tc := range testCase {
50
                // Read the image.
51
                m0, err := readPng(tc.filename)
52
                if err != nil {
53
                        t.Error(tc.filename, err)
54
                        continue
55
                }
56
                // Encode that image as JPEG.
57
                var buf bytes.Buffer
58
                err = Encode(&buf, m0, &Options{Quality: tc.quality})
59
                if err != nil {
60
                        t.Error(tc.filename, err)
61
                        continue
62
                }
63
                // Decode that JPEG.
64
                m1, err := Decode(&buf)
65
                if err != nil {
66
                        t.Error(tc.filename, err)
67
                        continue
68
                }
69
                // Compute the average delta in RGB space.
70
                b := m0.Bounds()
71
                var sum, n int64
72
                for y := b.Min.Y; y < b.Max.Y; y++ {
73
                        for x := b.Min.X; x < b.Max.X; x++ {
74
                                c0 := m0.At(x, y)
75
                                c1 := m1.At(x, y)
76
                                r0, g0, b0, _ := c0.RGBA()
77
                                r1, g1, b1, _ := c1.RGBA()
78
                                sum += delta(r0, r1)
79
                                sum += delta(g0, g1)
80
                                sum += delta(b0, b1)
81
                                n += 3
82
                        }
83
                }
84
                // Compare the average delta to the tolerance level.
85
                if sum/n > tc.tolerance {
86
                        t.Errorf("%s, quality=%d: average delta is too high", tc.filename, tc.quality)
87
                        continue
88
                }
89
        }
90
}
91
 
92
func BenchmarkEncodeRGBOpaque(b *testing.B) {
93
        b.StopTimer()
94
        img := image.NewRGBA(image.Rect(0, 0, 640, 480))
95
        // Set all pixels to 0xFF alpha to force opaque mode.
96
        bo := img.Bounds()
97
        rnd := rand.New(rand.NewSource(123))
98
        for y := bo.Min.Y; y < bo.Max.Y; y++ {
99
                for x := bo.Min.X; x < bo.Max.X; x++ {
100
                        img.Set(x, y, color.RGBA{
101
                                uint8(rnd.Intn(256)),
102
                                uint8(rnd.Intn(256)),
103
                                uint8(rnd.Intn(256)),
104
                                255})
105
                }
106
        }
107
        if !img.Opaque() {
108
                b.Fatal("expected image to be opaque")
109
        }
110
        b.SetBytes(640 * 480 * 4)
111
        b.StartTimer()
112
        options := &Options{Quality: 90}
113
        for i := 0; i < b.N; i++ {
114
                Encode(ioutil.Discard, img, options)
115
        }
116
}

powered by: WebSVN 2.1.0

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