~ubuntu-branches/ubuntu/saucy/golang/saucy

« back to all changes in this revision

Viewing changes to src/pkg/image/jpeg/writer_test.go

  • Committer: Package Import Robot
  • Author(s): Adam Conrad
  • Date: 2013-07-08 05:52:37 UTC
  • mfrom: (29.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130708055237-at01839e0hp8z3ni
Tags: 2:1.1-1ubuntu1
016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 
7
7
import (
8
8
        "bytes"
 
9
        "fmt"
9
10
        "image"
10
11
        "image/color"
11
12
        "image/png"
15
16
        "testing"
16
17
)
17
18
 
 
19
// zigzag maps from the natural ordering to the zig-zag ordering. For example,
 
20
// zigzag[0*8 + 3] is the zig-zag sequence number of the element in the fourth
 
21
// column and first row.
 
22
var zigzag = [blockSize]int{
 
23
        0, 1, 5, 6, 14, 15, 27, 28,
 
24
        2, 4, 7, 13, 16, 26, 29, 42,
 
25
        3, 8, 12, 17, 25, 30, 41, 43,
 
26
        9, 11, 18, 24, 31, 40, 44, 53,
 
27
        10, 19, 23, 32, 39, 45, 52, 54,
 
28
        20, 22, 33, 38, 46, 51, 55, 60,
 
29
        21, 34, 37, 47, 50, 56, 59, 61,
 
30
        35, 36, 48, 49, 57, 58, 62, 63,
 
31
}
 
32
 
 
33
func TestZigUnzig(t *testing.T) {
 
34
        for i := 0; i < blockSize; i++ {
 
35
                if unzig[zigzag[i]] != i {
 
36
                        t.Errorf("unzig[zigzag[%d]] == %d", i, unzig[zigzag[i]])
 
37
                }
 
38
                if zigzag[unzig[i]] != i {
 
39
                        t.Errorf("zigzag[unzig[%d]] == %d", i, zigzag[unzig[i]])
 
40
                }
 
41
        }
 
42
}
 
43
 
 
44
// unscaledQuantInNaturalOrder are the unscaled quantization tables in
 
45
// natural (not zig-zag) order, as specified in section K.1.
 
46
var unscaledQuantInNaturalOrder = [nQuantIndex][blockSize]byte{
 
47
        // Luminance.
 
48
        {
 
49
                16, 11, 10, 16, 24, 40, 51, 61,
 
50
                12, 12, 14, 19, 26, 58, 60, 55,
 
51
                14, 13, 16, 24, 40, 57, 69, 56,
 
52
                14, 17, 22, 29, 51, 87, 80, 62,
 
53
                18, 22, 37, 56, 68, 109, 103, 77,
 
54
                24, 35, 55, 64, 81, 104, 113, 92,
 
55
                49, 64, 78, 87, 103, 121, 120, 101,
 
56
                72, 92, 95, 98, 112, 100, 103, 99,
 
57
        },
 
58
        // Chrominance.
 
59
        {
 
60
                17, 18, 24, 47, 99, 99, 99, 99,
 
61
                18, 21, 26, 66, 99, 99, 99, 99,
 
62
                24, 26, 56, 99, 99, 99, 99, 99,
 
63
                47, 66, 99, 99, 99, 99, 99, 99,
 
64
                99, 99, 99, 99, 99, 99, 99, 99,
 
65
                99, 99, 99, 99, 99, 99, 99, 99,
 
66
                99, 99, 99, 99, 99, 99, 99, 99,
 
67
                99, 99, 99, 99, 99, 99, 99, 99,
 
68
        },
 
69
}
 
70
 
 
71
func TestUnscaledQuant(t *testing.T) {
 
72
        bad := false
 
73
        for i := quantIndex(0); i < nQuantIndex; i++ {
 
74
                for zig := 0; zig < blockSize; zig++ {
 
75
                        got := unscaledQuant[i][zig]
 
76
                        want := unscaledQuantInNaturalOrder[i][unzig[zig]]
 
77
                        if got != want {
 
78
                                t.Errorf("i=%d, zig=%d: got %d, want %d", i, zig, got, want)
 
79
                                bad = true
 
80
                        }
 
81
                }
 
82
        }
 
83
        if bad {
 
84
                names := [nQuantIndex]string{"Luminance", "Chrominance"}
 
85
                buf := &bytes.Buffer{}
 
86
                for i, name := range names {
 
87
                        fmt.Fprintf(buf, "// %s.\n{\n", name)
 
88
                        for zig := 0; zig < blockSize; zig++ {
 
89
                                fmt.Fprintf(buf, "%d, ", unscaledQuantInNaturalOrder[i][unzig[zig]])
 
90
                                if zig%8 == 7 {
 
91
                                        buf.WriteString("\n")
 
92
                                }
 
93
                        }
 
94
                        buf.WriteString("},\n")
 
95
                }
 
96
                t.Logf("expected unscaledQuant values:\n%s", buf.String())
 
97
        }
 
98
}
 
99
 
18
100
var testCase = []struct {
19
101
        filename  string
20
102
        quality   int
66
148
                        t.Error(tc.filename, err)
67
149
                        continue
68
150
                }
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
 
                        }
 
151
                if m0.Bounds() != m1.Bounds() {
 
152
                        t.Errorf("%s, bounds differ: %v and %v", tc.filename, m0.Bounds(), m1.Bounds())
 
153
                        continue
83
154
                }
84
155
                // Compare the average delta to the tolerance level.
85
 
                if sum/n > tc.tolerance {
 
156
                if averageDelta(m0, m1) > tc.tolerance {
86
157
                        t.Errorf("%s, quality=%d: average delta is too high", tc.filename, tc.quality)
87
158
                        continue
88
159
                }
89
160
        }
90
161
}
91
162
 
92
 
func BenchmarkEncodeRGBOpaque(b *testing.B) {
 
163
// averageDelta returns the average delta in RGB space. The two images must
 
164
// have the same bounds.
 
165
func averageDelta(m0, m1 image.Image) int64 {
 
166
        b := m0.Bounds()
 
167
        var sum, n int64
 
168
        for y := b.Min.Y; y < b.Max.Y; y++ {
 
169
                for x := b.Min.X; x < b.Max.X; x++ {
 
170
                        c0 := m0.At(x, y)
 
171
                        c1 := m1.At(x, y)
 
172
                        r0, g0, b0, _ := c0.RGBA()
 
173
                        r1, g1, b1, _ := c1.RGBA()
 
174
                        sum += delta(r0, r1)
 
175
                        sum += delta(g0, g1)
 
176
                        sum += delta(b0, b1)
 
177
                        n += 3
 
178
                }
 
179
        }
 
180
        return sum / n
 
181
}
 
182
 
 
183
func BenchmarkEncode(b *testing.B) {
93
184
        b.StopTimer()
94
185
        img := image.NewRGBA(image.Rect(0, 0, 640, 480))
95
 
        // Set all pixels to 0xFF alpha to force opaque mode.
96
186
        bo := img.Bounds()
97
187
        rnd := rand.New(rand.NewSource(123))
98
188
        for y := bo.Min.Y; y < bo.Max.Y; y++ {
99
189
                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})
 
190
                        img.SetRGBA(x, y, color.RGBA{
 
191
                                uint8(rnd.Intn(256)),
 
192
                                uint8(rnd.Intn(256)),
 
193
                                uint8(rnd.Intn(256)),
 
194
                                255,
 
195
                        })
105
196
                }
106
197
        }
107
 
        if !img.Opaque() {
108
 
                b.Fatal("expected image to be opaque")
109
 
        }
110
198
        b.SetBytes(640 * 480 * 4)
111
199
        b.StartTimer()
112
200
        options := &Options{Quality: 90}