2009-10-08 00:14:23 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package png
|
|
|
|
|
|
|
|
import (
|
2009-12-15 16:35:38 -07:00
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
"io"
|
2011-04-29 11:42:44 -06:00
|
|
|
"io/ioutil"
|
2009-12-15 16:35:38 -07:00
|
|
|
"os"
|
|
|
|
"testing"
|
2009-10-08 00:14:23 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func diff(m0, m1 image.Image) os.Error {
|
2010-08-09 20:08:52 -06:00
|
|
|
b0, b1 := m0.Bounds(), m1.Bounds()
|
|
|
|
if !b0.Eq(b1) {
|
2010-09-30 22:14:18 -06:00
|
|
|
return fmt.Errorf("dimensions differ: %v vs %v", b0, b1)
|
2009-10-08 00:14:23 -06:00
|
|
|
}
|
2010-08-09 20:08:52 -06:00
|
|
|
for y := b0.Min.Y; y < b0.Max.Y; y++ {
|
|
|
|
for x := b0.Min.X; x < b0.Max.X; x++ {
|
2009-12-15 16:35:38 -07:00
|
|
|
r0, g0, b0, a0 := m0.At(x, y).RGBA()
|
|
|
|
r1, g1, b1, a1 := m1.At(x, y).RGBA()
|
2009-10-08 00:14:23 -06:00
|
|
|
if r0 != r1 || g0 != g1 || b0 != b1 || a0 != a1 {
|
2010-09-30 22:14:18 -06:00
|
|
|
return fmt.Errorf("colors differ at (%d, %d): %v vs %v", x, y, m0.At(x, y), m1.At(x, y))
|
2009-10-08 00:14:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return nil
|
2009-10-08 00:14:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriter(t *testing.T) {
|
|
|
|
// The filenames variable is declared in reader_test.go.
|
2011-03-26 12:25:22 -06:00
|
|
|
names := filenames
|
|
|
|
if testing.Short() {
|
|
|
|
names = filenamesShort
|
|
|
|
}
|
|
|
|
for _, fn := range names {
|
2009-12-15 16:35:38 -07:00
|
|
|
qfn := "testdata/pngsuite/" + fn + ".png"
|
2009-10-08 00:14:23 -06:00
|
|
|
// Read the image.
|
2009-12-15 16:35:38 -07:00
|
|
|
m0, err := readPng(qfn)
|
2009-10-08 00:14:23 -06:00
|
|
|
if err != nil {
|
2009-12-15 16:35:38 -07:00
|
|
|
t.Error(fn, err)
|
|
|
|
continue
|
2009-10-08 00:14:23 -06:00
|
|
|
}
|
|
|
|
// Read the image again, and push it through a pipe that encodes at the write end, and decodes at the read end.
|
2009-12-15 16:35:38 -07:00
|
|
|
pr, pw := io.Pipe()
|
|
|
|
defer pr.Close()
|
2009-10-08 00:14:23 -06:00
|
|
|
go func() {
|
2009-12-15 16:35:38 -07:00
|
|
|
defer pw.Close()
|
|
|
|
m1, err := readPng(qfn)
|
2009-10-08 00:14:23 -06:00
|
|
|
if err != nil {
|
2009-12-15 16:35:38 -07:00
|
|
|
t.Error(fn, err)
|
|
|
|
return
|
2009-10-08 00:14:23 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
err = Encode(pw, m1)
|
2009-10-08 00:14:23 -06:00
|
|
|
if err != nil {
|
2009-12-15 16:35:38 -07:00
|
|
|
t.Error(fn, err)
|
|
|
|
return
|
2009-10-08 00:14:23 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
}()
|
|
|
|
m2, err := Decode(pr)
|
2009-10-08 00:14:23 -06:00
|
|
|
if err != nil {
|
2009-12-15 16:35:38 -07:00
|
|
|
t.Error(fn, err)
|
|
|
|
continue
|
2009-10-08 00:14:23 -06:00
|
|
|
}
|
|
|
|
// Compare the two.
|
2009-12-15 16:35:38 -07:00
|
|
|
err = diff(m0, m2)
|
2009-10-08 00:14:23 -06:00
|
|
|
if err != nil {
|
2009-12-15 16:35:38 -07:00
|
|
|
t.Error(fn, err)
|
|
|
|
continue
|
2009-10-08 00:14:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-27 05:48:18 -06:00
|
|
|
|
|
|
|
func BenchmarkEncodePaletted(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
img := image.NewPaletted(640, 480,
|
|
|
|
[]image.Color{
|
|
|
|
image.RGBAColor{0, 0, 0, 255},
|
|
|
|
image.RGBAColor{255, 255, 255, 255},
|
|
|
|
})
|
2011-04-29 11:42:44 -06:00
|
|
|
b.SetBytes(640 * 480 * 1)
|
2010-10-27 05:48:18 -06:00
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2011-04-29 11:42:44 -06:00
|
|
|
Encode(ioutil.Discard, img)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEncodeRGBOpaque(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
img := image.NewRGBA(640, 480)
|
|
|
|
// Set all pixels to 0xFF alpha to force opaque mode.
|
|
|
|
bo := img.Bounds()
|
|
|
|
for y := bo.Min.Y; y < bo.Max.Y; y++ {
|
|
|
|
for x := bo.Min.X; x < bo.Max.X; x++ {
|
|
|
|
img.Set(x, y, image.RGBAColor{0, 0, 0, 255})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !img.Opaque() {
|
|
|
|
panic("expected image to be opaque")
|
|
|
|
}
|
|
|
|
b.SetBytes(640 * 480 * 4)
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
Encode(ioutil.Discard, img)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEncodeRGBA(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
img := image.NewRGBA(640, 480)
|
|
|
|
if img.Opaque() {
|
|
|
|
panic("expected image to not be opaque")
|
|
|
|
}
|
|
|
|
b.SetBytes(640 * 480 * 4)
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
Encode(ioutil.Discard, img)
|
2010-10-27 05:48:18 -06:00
|
|
|
}
|
|
|
|
}
|