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.
16 func diff(m0, m1 image.Image) os.Error {
17 b0, b1 := m0.Bounds(), m1.Bounds()
18 if !b0.Size().Eq(b1.Size()) {
19 return fmt.Errorf("dimensions differ: %v vs %v", b0, b1)
21 dx := b1.Min.X - b0.Min.X
22 dy := b1.Min.Y - b0.Min.Y
23 for y := b0.Min.Y; y < b0.Max.Y; y++ {
24 for x := b0.Min.X; x < b0.Max.X; x++ {
26 c1 := m1.At(x+dx, y+dy)
27 r0, g0, b0, a0 := c0.RGBA()
28 r1, g1, b1, a1 := c1.RGBA()
29 if r0 != r1 || g0 != g1 || b0 != b1 || a0 != a1 {
30 return fmt.Errorf("colors differ at (%d, %d): %v vs %v", x, y, c0, c1)
37 func encodeDecode(m image.Image) (image.Image, os.Error) {
38 b := bytes.NewBuffer(nil)
50 func TestWriter(t *testing.T) {
51 // The filenames variable is declared in reader_test.go.
54 names = filenamesShort
56 for _, fn := range names {
57 qfn := "testdata/pngsuite/" + fn + ".png"
59 m0, err := readPng(qfn)
64 // Read the image again, encode it, and decode it.
65 m1, err := readPng(qfn)
70 m2, err := encodeDecode(m1)
84 func TestSubImage(t *testing.T) {
85 m0 := image.NewRGBA(256, 256)
86 for y := 0; y < 256; y++ {
87 for x := 0; x < 256; x++ {
88 m0.Set(x, y, image.RGBAColor{uint8(x), uint8(y), 0, 255})
91 m0 = m0.SubImage(image.Rect(50, 30, 250, 130)).(*image.RGBA)
92 m1, err := encodeDecode(m0)
104 func BenchmarkEncodePaletted(b *testing.B) {
106 img := image.NewPaletted(640, 480,
108 image.RGBAColor{0, 0, 0, 255},
109 image.RGBAColor{255, 255, 255, 255},
111 b.SetBytes(640 * 480 * 1)
113 for i := 0; i < b.N; i++ {
114 Encode(ioutil.Discard, img)
118 func BenchmarkEncodeRGBOpaque(b *testing.B) {
120 img := image.NewRGBA(640, 480)
121 // Set all pixels to 0xFF alpha to force opaque mode.
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, image.RGBAColor{0, 0, 0, 255})
129 panic("expected image to be opaque")
131 b.SetBytes(640 * 480 * 4)
133 for i := 0; i < b.N; i++ {
134 Encode(ioutil.Discard, img)
138 func BenchmarkEncodeRGBA(b *testing.B) {
140 img := image.NewRGBA(640, 480)
142 panic("expected image to not be opaque")
144 b.SetBytes(640 * 480 * 4)
146 for i := 0; i < b.N; i++ {
147 Encode(ioutil.Discard, img)