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.
18 var testCase = []struct {
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},
31 func delta(u0, u1 uint32) int64 {
32 d := int64(u0) - int64(u1)
39 func readPng(filename string) (image.Image, error) {
40 f, err := os.Open(filename)
48 func TestWriter(t *testing.T) {
49 for _, tc := range testCase {
51 m0, err := readPng(tc.filename)
53 t.Error(tc.filename, err)
56 // Encode that image as JPEG.
58 err = Encode(&buf, m0, &Options{Quality: tc.quality})
60 t.Error(tc.filename, err)
64 m1, err := Decode(&buf)
66 t.Error(tc.filename, err)
69 // Compute the average delta in RGB space.
72 for y := b.Min.Y; y < b.Max.Y; y++ {
73 for x := b.Min.X; x < b.Max.X; x++ {
76 r0, g0, b0, _ := c0.RGBA()
77 r1, g1, b1, _ := c1.RGBA()
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)
92 func BenchmarkEncodeRGBOpaque(b *testing.B) {
94 img := image.NewRGBA(image.Rect(0, 0, 640, 480))
95 // Set all pixels to 0xFF alpha to force opaque mode.
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)),
108 b.Fatal("expected image to be opaque")
110 b.SetBytes(640 * 480 * 4)
112 options := &Options{Quality: 90}
113 for i := 0; i < b.N; i++ {
114 Encode(ioutil.Discard, img, options)