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