github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/image/png/writer_test.go (about)

     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  	"testing"
    14  )
    15  
    16  func diff(m0, m1 image.Image) 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)
    20  	}
    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++ {
    25  			c0 := m0.At(x, y)
    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)
    31  			}
    32  		}
    33  	}
    34  	return nil
    35  }
    36  
    37  func encodeDecode(m image.Image) (image.Image, error) {
    38  	var b bytes.Buffer
    39  	err := Encode(&b, m)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	m, err = Decode(&b)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	return m, nil
    48  }
    49  
    50  func TestWriter(t *testing.T) {
    51  	// The filenames variable is declared in reader_test.go.
    52  	names := filenames
    53  	if testing.Short() {
    54  		names = filenamesShort
    55  	}
    56  	for _, fn := range names {
    57  		qfn := "testdata/pngsuite/" + fn + ".png"
    58  		// Read the image.
    59  		m0, err := readPNG(qfn)
    60  		if err != nil {
    61  			t.Error(fn, err)
    62  			continue
    63  		}
    64  		// Read the image again, encode it, and decode it.
    65  		m1, err := readPNG(qfn)
    66  		if err != nil {
    67  			t.Error(fn, err)
    68  			return
    69  		}
    70  		m2, err := encodeDecode(m1)
    71  		if err != nil {
    72  			t.Error(fn, err)
    73  			return
    74  		}
    75  		// Compare the two.
    76  		err = diff(m0, m2)
    77  		if err != nil {
    78  			t.Error(fn, err)
    79  			continue
    80  		}
    81  	}
    82  }
    83  
    84  func TestSubImage(t *testing.T) {
    85  	m0 := image.NewRGBA(image.Rect(0, 0, 256, 256))
    86  	for y := 0; y < 256; y++ {
    87  		for x := 0; x < 256; x++ {
    88  			m0.Set(x, y, color.RGBA{uint8(x), uint8(y), 0, 255})
    89  		}
    90  	}
    91  	m0 = m0.SubImage(image.Rect(50, 30, 250, 130)).(*image.RGBA)
    92  	m1, err := encodeDecode(m0)
    93  	if err != nil {
    94  		t.Error(err)
    95  		return
    96  	}
    97  	err = diff(m0, m1)
    98  	if err != nil {
    99  		t.Error(err)
   100  		return
   101  	}
   102  }
   103  
   104  func BenchmarkEncodeGray(b *testing.B) {
   105  	b.StopTimer()
   106  	img := image.NewGray(image.Rect(0, 0, 640, 480))
   107  	b.SetBytes(640 * 480 * 1)
   108  	b.StartTimer()
   109  	for i := 0; i < b.N; i++ {
   110  		Encode(ioutil.Discard, img)
   111  	}
   112  }
   113  
   114  func BenchmarkEncodeNRGBOpaque(b *testing.B) {
   115  	b.StopTimer()
   116  	img := image.NewNRGBA(image.Rect(0, 0, 640, 480))
   117  	// Set all pixels to 0xFF alpha to force opaque mode.
   118  	bo := img.Bounds()
   119  	for y := bo.Min.Y; y < bo.Max.Y; y++ {
   120  		for x := bo.Min.X; x < bo.Max.X; x++ {
   121  			img.Set(x, y, color.NRGBA{0, 0, 0, 255})
   122  		}
   123  	}
   124  	if !img.Opaque() {
   125  		b.Fatal("expected image to be opaque")
   126  	}
   127  	b.SetBytes(640 * 480 * 4)
   128  	b.StartTimer()
   129  	for i := 0; i < b.N; i++ {
   130  		Encode(ioutil.Discard, img)
   131  	}
   132  }
   133  
   134  func BenchmarkEncodeNRGBA(b *testing.B) {
   135  	b.StopTimer()
   136  	img := image.NewNRGBA(image.Rect(0, 0, 640, 480))
   137  	if img.Opaque() {
   138  		b.Fatal("expected image not to be opaque")
   139  	}
   140  	b.SetBytes(640 * 480 * 4)
   141  	b.StartTimer()
   142  	for i := 0; i < b.N; i++ {
   143  		Encode(ioutil.Discard, img)
   144  	}
   145  }
   146  
   147  func BenchmarkEncodePaletted(b *testing.B) {
   148  	b.StopTimer()
   149  	img := image.NewPaletted(image.Rect(0, 0, 640, 480), color.Palette{
   150  		color.RGBA{0, 0, 0, 255},
   151  		color.RGBA{255, 255, 255, 255},
   152  	})
   153  	b.SetBytes(640 * 480 * 1)
   154  	b.StartTimer()
   155  	for i := 0; i < b.N; i++ {
   156  		Encode(ioutil.Discard, img)
   157  	}
   158  }
   159  
   160  func BenchmarkEncodeRGBOpaque(b *testing.B) {
   161  	b.StopTimer()
   162  	img := image.NewRGBA(image.Rect(0, 0, 640, 480))
   163  	// Set all pixels to 0xFF alpha to force opaque mode.
   164  	bo := img.Bounds()
   165  	for y := bo.Min.Y; y < bo.Max.Y; y++ {
   166  		for x := bo.Min.X; x < bo.Max.X; x++ {
   167  			img.Set(x, y, color.RGBA{0, 0, 0, 255})
   168  		}
   169  	}
   170  	if !img.Opaque() {
   171  		b.Fatal("expected image to be opaque")
   172  	}
   173  	b.SetBytes(640 * 480 * 4)
   174  	b.StartTimer()
   175  	for i := 0; i < b.N; i++ {
   176  		Encode(ioutil.Discard, img)
   177  	}
   178  }
   179  
   180  func BenchmarkEncodeRGBA(b *testing.B) {
   181  	b.StopTimer()
   182  	img := image.NewRGBA(image.Rect(0, 0, 640, 480))
   183  	if img.Opaque() {
   184  		b.Fatal("expected image not to be opaque")
   185  	}
   186  	b.SetBytes(640 * 480 * 4)
   187  	b.StartTimer()
   188  	for i := 0; i < b.N; i++ {
   189  		Encode(ioutil.Discard, img)
   190  	}
   191  }