OSDN Git Service

6b054aaa893c9c0754de033664b7f280789b7cea
[pf3gnuchains/gcc-fork.git] / libgo / go / image / png / writer_test.go
1 // Copyright 2009 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 png
6
7 import (
8         "fmt"
9         "image"
10         "io"
11         "io/ioutil"
12         "os"
13         "testing"
14 )
15
16 func diff(m0, m1 image.Image) os.Error {
17         b0, b1 := m0.Bounds(), m1.Bounds()
18         if !b0.Eq(b1) {
19                 return fmt.Errorf("dimensions differ: %v vs %v", b0, b1)
20         }
21         for y := b0.Min.Y; y < b0.Max.Y; y++ {
22                 for x := b0.Min.X; x < b0.Max.X; x++ {
23                         r0, g0, b0, a0 := m0.At(x, y).RGBA()
24                         r1, g1, b1, a1 := m1.At(x, y).RGBA()
25                         if r0 != r1 || g0 != g1 || b0 != b1 || a0 != a1 {
26                                 return fmt.Errorf("colors differ at (%d, %d): %v vs %v", x, y, m0.At(x, y), m1.At(x, y))
27                         }
28                 }
29         }
30         return nil
31 }
32
33 func TestWriter(t *testing.T) {
34         // The filenames variable is declared in reader_test.go.
35         names := filenames
36         if testing.Short() {
37                 names = filenamesShort
38         }
39         for _, fn := range names {
40                 qfn := "testdata/pngsuite/" + fn + ".png"
41                 // Read the image.
42                 m0, err := readPng(qfn)
43                 if err != nil {
44                         t.Error(fn, err)
45                         continue
46                 }
47                 // Read the image again, and push it through a pipe that encodes at the write end, and decodes at the read end.
48                 pr, pw := io.Pipe()
49                 defer pr.Close()
50                 go func() {
51                         defer pw.Close()
52                         m1, err := readPng(qfn)
53                         if err != nil {
54                                 t.Error(fn, err)
55                                 return
56                         }
57                         err = Encode(pw, m1)
58                         if err != nil {
59                                 t.Error(fn, err)
60                                 return
61                         }
62                 }()
63                 m2, err := Decode(pr)
64                 if err != nil {
65                         t.Error(fn, err)
66                         continue
67                 }
68                 // Compare the two.
69                 err = diff(m0, m2)
70                 if err != nil {
71                         t.Error(fn, err)
72                         continue
73                 }
74         }
75 }
76
77 func BenchmarkEncodePaletted(b *testing.B) {
78         b.StopTimer()
79         img := image.NewPaletted(640, 480,
80                 []image.Color{
81                         image.RGBAColor{0, 0, 0, 255},
82                         image.RGBAColor{255, 255, 255, 255},
83                 })
84         b.SetBytes(640 * 480 * 1)
85         b.StartTimer()
86         for i := 0; i < b.N; i++ {
87                 Encode(ioutil.Discard, img)
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         for y := bo.Min.Y; y < bo.Max.Y; y++ {
97                 for x := bo.Min.X; x < bo.Max.X; x++ {
98                         img.Set(x, y, image.RGBAColor{0, 0, 0, 255})
99                 }
100         }
101         if !img.Opaque() {
102                 panic("expected image to be opaque")
103         }
104         b.SetBytes(640 * 480 * 4)
105         b.StartTimer()
106         for i := 0; i < b.N; i++ {
107                 Encode(ioutil.Discard, img)
108         }
109 }
110
111 func BenchmarkEncodeRGBA(b *testing.B) {
112         b.StopTimer()
113         img := image.NewRGBA(640, 480)
114         if img.Opaque() {
115                 panic("expected image to not be opaque")
116         }
117         b.SetBytes(640 * 480 * 4)
118         b.StartTimer()
119         for i := 0; i < b.N; i++ {
120                 Encode(ioutil.Discard, img)
121         }
122 }