]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgo/go/image/png/writer_test.go
Add Go frontend, libgo library, and Go testsuite.
[thirdparty/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 "io"
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 for _, fn := range filenames {
36 qfn := "testdata/pngsuite/" + fn + ".png"
37 // Read the image.
38 m0, err := readPng(qfn)
39 if err != nil {
40 t.Error(fn, err)
41 continue
42 }
43 // Read the image again, and push it through a pipe that encodes at the write end, and decodes at the read end.
44 pr, pw := io.Pipe()
45 defer pr.Close()
46 go func() {
47 defer pw.Close()
48 m1, err := readPng(qfn)
49 if err != nil {
50 t.Error(fn, err)
51 return
52 }
53 err = Encode(pw, m1)
54 if err != nil {
55 t.Error(fn, err)
56 return
57 }
58 }()
59 m2, err := Decode(pr)
60 if err != nil {
61 t.Error(fn, err)
62 continue
63 }
64 // Compare the two.
65 err = diff(m0, m2)
66 if err != nil {
67 t.Error(fn, err)
68 continue
69 }
70 }
71 }
72
73 func BenchmarkEncodePaletted(b *testing.B) {
74 b.StopTimer()
75 img := image.NewPaletted(640, 480,
76 []image.Color{
77 image.RGBAColor{0, 0, 0, 255},
78 image.RGBAColor{255, 255, 255, 255},
79 })
80 b.StartTimer()
81 buffer := new(bytes.Buffer)
82 for i := 0; i < b.N; i++ {
83 buffer.Reset()
84 Encode(buffer, img)
85 }
86 }