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.
17 var testCase = []struct {
22 {"../testdata/video-001.png", 1, 24 << 8},
23 {"../testdata/video-001.png", 20, 12 << 8},
24 {"../testdata/video-001.png", 60, 8 << 8},
25 {"../testdata/video-001.png", 80, 6 << 8},
26 {"../testdata/video-001.png", 90, 4 << 8},
27 {"../testdata/video-001.png", 100, 2 << 8},
30 func delta(u0, u1 uint32) int64 {
31 d := int64(u0) - int64(u1)
38 func readPng(filename string) (image.Image, os.Error) {
39 f, err := os.Open(filename)
47 func TestWriter(t *testing.T) {
48 for _, tc := range testCase {
50 m0, err := readPng(tc.filename)
52 t.Error(tc.filename, err)
55 // Encode that image as JPEG.
56 buf := bytes.NewBuffer(nil)
57 err = Encode(buf, m0, &Options{Quality: tc.quality})
59 t.Error(tc.filename, err)
63 m1, err := Decode(buf)
65 t.Error(tc.filename, err)
68 // Compute the average delta in RGB space.
71 for y := b.Min.Y; y < b.Max.Y; y++ {
72 for x := b.Min.X; x < b.Max.X; x++ {
75 r0, g0, b0, _ := c0.RGBA()
76 r1, g1, b1, _ := c1.RGBA()
83 // Compare the average delta to the tolerance level.
84 if sum/n > tc.tolerance {
85 t.Errorf("%s, quality=%d: average delta is too high", tc.filename, tc.quality)
91 func BenchmarkEncodeRGBOpaque(b *testing.B) {
93 img := image.NewRGBA(640, 480)
94 // Set all pixels to 0xFF alpha to force opaque mode.
96 rnd := rand.New(rand.NewSource(123))
97 for y := bo.Min.Y; y < bo.Max.Y; y++ {
98 for x := bo.Min.X; x < bo.Max.X; x++ {
99 img.Set(x, y, image.RGBAColor{
100 uint8(rnd.Intn(256)),
101 uint8(rnd.Intn(256)),
102 uint8(rnd.Intn(256)),
107 panic("expected image to be opaque")
109 b.SetBytes(640 * 480 * 4)
111 options := &Options{Quality: 90}
112 for i := 0; i < b.N; i++ {
113 Encode(ioutil.Discard, img, options)