Home | History | Annotate | Download | only in png
      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 	return Decode(&b)
     44 }
     45 
     46 func TestWriter(t *testing.T) {
     47 	// The filenames variable is declared in reader_test.go.
     48 	names := filenames
     49 	if testing.Short() {
     50 		names = filenamesShort
     51 	}
     52 	for _, fn := range names {
     53 		qfn := "testdata/pngsuite/" + fn + ".png"
     54 		// Read the image.
     55 		m0, err := readPNG(qfn)
     56 		if err != nil {
     57 			t.Error(fn, err)
     58 			continue
     59 		}
     60 		// Read the image again, encode it, and decode it.
     61 		m1, err := readPNG(qfn)
     62 		if err != nil {
     63 			t.Error(fn, err)
     64 			return
     65 		}
     66 		m2, err := encodeDecode(m1)
     67 		if err != nil {
     68 			t.Error(fn, err)
     69 			return
     70 		}
     71 		// Compare the two.
     72 		err = diff(m0, m2)
     73 		if err != nil {
     74 			t.Error(fn, err)
     75 			continue
     76 		}
     77 	}
     78 }
     79 
     80 func TestWriterLevels(t *testing.T) {
     81 	m := image.NewNRGBA(image.Rect(0, 0, 100, 100))
     82 
     83 	var b1, b2 bytes.Buffer
     84 	if err := (&Encoder{}).Encode(&b1, m); err != nil {
     85 		t.Fatal(err)
     86 	}
     87 	noenc := &Encoder{CompressionLevel: NoCompression}
     88 	if err := noenc.Encode(&b2, m); err != nil {
     89 		t.Fatal(err)
     90 	}
     91 
     92 	if b2.Len() <= b1.Len() {
     93 		t.Error("DefaultCompression encoding was larger than NoCompression encoding")
     94 	}
     95 	if _, err := Decode(&b1); err != nil {
     96 		t.Error("cannot decode DefaultCompression")
     97 	}
     98 	if _, err := Decode(&b2); err != nil {
     99 		t.Error("cannot decode NoCompression")
    100 	}
    101 }
    102 
    103 func TestSubImage(t *testing.T) {
    104 	m0 := image.NewRGBA(image.Rect(0, 0, 256, 256))
    105 	for y := 0; y < 256; y++ {
    106 		for x := 0; x < 256; x++ {
    107 			m0.Set(x, y, color.RGBA{uint8(x), uint8(y), 0, 255})
    108 		}
    109 	}
    110 	m0 = m0.SubImage(image.Rect(50, 30, 250, 130)).(*image.RGBA)
    111 	m1, err := encodeDecode(m0)
    112 	if err != nil {
    113 		t.Error(err)
    114 		return
    115 	}
    116 	err = diff(m0, m1)
    117 	if err != nil {
    118 		t.Error(err)
    119 		return
    120 	}
    121 }
    122 
    123 func BenchmarkEncodeGray(b *testing.B) {
    124 	img := image.NewGray(image.Rect(0, 0, 640, 480))
    125 	b.SetBytes(640 * 480 * 1)
    126 	b.ReportAllocs()
    127 	b.ResetTimer()
    128 	for i := 0; i < b.N; i++ {
    129 		Encode(ioutil.Discard, img)
    130 	}
    131 }
    132 
    133 type pool struct {
    134 	b *EncoderBuffer
    135 }
    136 
    137 func (p *pool) Get() *EncoderBuffer {
    138 	return p.b
    139 }
    140 
    141 func (p *pool) Put(b *EncoderBuffer) {
    142 	p.b = b
    143 }
    144 
    145 func BenchmarkEncodeGrayWithBufferPool(b *testing.B) {
    146 	img := image.NewGray(image.Rect(0, 0, 640, 480))
    147 	e := Encoder{
    148 		BufferPool: &pool{},
    149 	}
    150 	b.SetBytes(640 * 480 * 1)
    151 	b.ReportAllocs()
    152 	b.ResetTimer()
    153 	for i := 0; i < b.N; i++ {
    154 		e.Encode(ioutil.Discard, img)
    155 	}
    156 }
    157 
    158 func BenchmarkEncodeNRGBOpaque(b *testing.B) {
    159 	img := image.NewNRGBA(image.Rect(0, 0, 640, 480))
    160 	// Set all pixels to 0xFF alpha to force opaque mode.
    161 	bo := img.Bounds()
    162 	for y := bo.Min.Y; y < bo.Max.Y; y++ {
    163 		for x := bo.Min.X; x < bo.Max.X; x++ {
    164 			img.Set(x, y, color.NRGBA{0, 0, 0, 255})
    165 		}
    166 	}
    167 	if !img.Opaque() {
    168 		b.Fatal("expected image to be opaque")
    169 	}
    170 	b.SetBytes(640 * 480 * 4)
    171 	b.ReportAllocs()
    172 	b.ResetTimer()
    173 	for i := 0; i < b.N; i++ {
    174 		Encode(ioutil.Discard, img)
    175 	}
    176 }
    177 
    178 func BenchmarkEncodeNRGBA(b *testing.B) {
    179 	img := image.NewNRGBA(image.Rect(0, 0, 640, 480))
    180 	if img.Opaque() {
    181 		b.Fatal("expected image not to be opaque")
    182 	}
    183 	b.SetBytes(640 * 480 * 4)
    184 	b.ReportAllocs()
    185 	b.ResetTimer()
    186 	for i := 0; i < b.N; i++ {
    187 		Encode(ioutil.Discard, img)
    188 	}
    189 }
    190 
    191 func BenchmarkEncodePaletted(b *testing.B) {
    192 	img := image.NewPaletted(image.Rect(0, 0, 640, 480), color.Palette{
    193 		color.RGBA{0, 0, 0, 255},
    194 		color.RGBA{255, 255, 255, 255},
    195 	})
    196 	b.SetBytes(640 * 480 * 1)
    197 	b.ReportAllocs()
    198 	b.ResetTimer()
    199 	for i := 0; i < b.N; i++ {
    200 		Encode(ioutil.Discard, img)
    201 	}
    202 }
    203 
    204 func BenchmarkEncodeRGBOpaque(b *testing.B) {
    205 	img := image.NewRGBA(image.Rect(0, 0, 640, 480))
    206 	// Set all pixels to 0xFF alpha to force opaque mode.
    207 	bo := img.Bounds()
    208 	for y := bo.Min.Y; y < bo.Max.Y; y++ {
    209 		for x := bo.Min.X; x < bo.Max.X; x++ {
    210 			img.Set(x, y, color.RGBA{0, 0, 0, 255})
    211 		}
    212 	}
    213 	if !img.Opaque() {
    214 		b.Fatal("expected image to be opaque")
    215 	}
    216 	b.SetBytes(640 * 480 * 4)
    217 	b.ReportAllocs()
    218 	b.ResetTimer()
    219 	for i := 0; i < b.N; i++ {
    220 		Encode(ioutil.Discard, img)
    221 	}
    222 }
    223 
    224 func BenchmarkEncodeRGBA(b *testing.B) {
    225 	img := image.NewRGBA(image.Rect(0, 0, 640, 480))
    226 	if img.Opaque() {
    227 		b.Fatal("expected image not to be opaque")
    228 	}
    229 	b.SetBytes(640 * 480 * 4)
    230 	b.ReportAllocs()
    231 	b.ResetTimer()
    232 	for i := 0; i < b.N; i++ {
    233 		Encode(ioutil.Discard, img)
    234 	}
    235 }
    236