Update Go library to last weekly.
[platform/upstream/gcc.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         "bytes"
9         "fmt"
10         "image"
11         "image/color"
12         "io/ioutil"
13         "os"
14         "testing"
15 )
16
17 func diff(m0, m1 image.Image) os.Error {
18         b0, b1 := m0.Bounds(), m1.Bounds()
19         if !b0.Size().Eq(b1.Size()) {
20                 return fmt.Errorf("dimensions differ: %v vs %v", b0, b1)
21         }
22         dx := b1.Min.X - b0.Min.X
23         dy := b1.Min.Y - b0.Min.Y
24         for y := b0.Min.Y; y < b0.Max.Y; y++ {
25                 for x := b0.Min.X; x < b0.Max.X; x++ {
26                         c0 := m0.At(x, y)
27                         c1 := m1.At(x+dx, y+dy)
28                         r0, g0, b0, a0 := c0.RGBA()
29                         r1, g1, b1, a1 := c1.RGBA()
30                         if r0 != r1 || g0 != g1 || b0 != b1 || a0 != a1 {
31                                 return fmt.Errorf("colors differ at (%d, %d): %v vs %v", x, y, c0, c1)
32                         }
33                 }
34         }
35         return nil
36 }
37
38 func encodeDecode(m image.Image) (image.Image, os.Error) {
39         b := bytes.NewBuffer(nil)
40         err := Encode(b, m)
41         if err != nil {
42                 return nil, err
43         }
44         m, err = Decode(b)
45         if err != nil {
46                 return nil, err
47         }
48         return m, nil
49 }
50
51 func TestWriter(t *testing.T) {
52         // The filenames variable is declared in reader_test.go.
53         names := filenames
54         if testing.Short() {
55                 names = filenamesShort
56         }
57         for _, fn := range names {
58                 qfn := "testdata/pngsuite/" + fn + ".png"
59                 // Read the image.
60                 m0, err := readPNG(qfn)
61                 if err != nil {
62                         t.Error(fn, err)
63                         continue
64                 }
65                 // Read the image again, encode it, and decode it.
66                 m1, err := readPNG(qfn)
67                 if err != nil {
68                         t.Error(fn, err)
69                         return
70                 }
71                 m2, err := encodeDecode(m1)
72                 if err != nil {
73                         t.Error(fn, err)
74                         return
75                 }
76                 // Compare the two.
77                 err = diff(m0, m2)
78                 if err != nil {
79                         t.Error(fn, err)
80                         continue
81                 }
82         }
83 }
84
85 func TestSubImage(t *testing.T) {
86         m0 := image.NewRGBA(image.Rect(0, 0, 256, 256))
87         for y := 0; y < 256; y++ {
88                 for x := 0; x < 256; x++ {
89                         m0.Set(x, y, color.RGBA{uint8(x), uint8(y), 0, 255})
90                 }
91         }
92         m0 = m0.SubImage(image.Rect(50, 30, 250, 130)).(*image.RGBA)
93         m1, err := encodeDecode(m0)
94         if err != nil {
95                 t.Error(err)
96                 return
97         }
98         err = diff(m0, m1)
99         if err != nil {
100                 t.Error(err)
101                 return
102         }
103 }
104
105 func BenchmarkEncodePaletted(b *testing.B) {
106         b.StopTimer()
107         img := image.NewPaletted(image.Rect(0, 0, 640, 480), color.Palette{
108                 color.RGBA{0, 0, 0, 255},
109                 color.RGBA{255, 255, 255, 255},
110         })
111         b.SetBytes(640 * 480 * 1)
112         b.StartTimer()
113         for i := 0; i < b.N; i++ {
114                 Encode(ioutil.Discard, img)
115         }
116 }
117
118 func BenchmarkEncodeRGBOpaque(b *testing.B) {
119         b.StopTimer()
120         img := image.NewRGBA(image.Rect(0, 0, 640, 480))
121         // Set all pixels to 0xFF alpha to force opaque mode.
122         bo := img.Bounds()
123         for y := bo.Min.Y; y < bo.Max.Y; y++ {
124                 for x := bo.Min.X; x < bo.Max.X; x++ {
125                         img.Set(x, y, color.RGBA{0, 0, 0, 255})
126                 }
127         }
128         if !img.Opaque() {
129                 panic("expected image to be opaque")
130         }
131         b.SetBytes(640 * 480 * 4)
132         b.StartTimer()
133         for i := 0; i < b.N; i++ {
134                 Encode(ioutil.Discard, img)
135         }
136 }
137
138 func BenchmarkEncodeRGBA(b *testing.B) {
139         b.StopTimer()
140         img := image.NewRGBA(image.Rect(0, 0, 640, 480))
141         if img.Opaque() {
142                 panic("expected image to not be opaque")
143         }
144         b.SetBytes(640 * 480 * 4)
145         b.StartTimer()
146         for i := 0; i < b.N; i++ {
147                 Encode(ioutil.Discard, img)
148         }
149 }