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
) 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
, error
) {
46 func TestWriter(t
*testing
.T
) {
47 // The filenames variable is declared in reader_test.go.
50 names
= filenamesShort
52 for _
, fn
:= range names
{
53 qfn
:= "testdata/pngsuite/" + fn
+ ".png"
55 m0
, err
:= readPNG(qfn
)
60 // Read the image again, encode it, and decode it.
61 m1
, err
:= readPNG(qfn
)
66 m2
, err
:= encodeDecode(m1
)
80 func TestWriterLevels(t
*testing
.T
) {
81 m
:= image
.NewNRGBA(image
.Rect(0, 0, 100, 100))
83 var b1
, b2 bytes
.Buffer
84 if err
:= (&Encoder
{}).Encode(&b1
, m
); err
!= nil {
87 noenc
:= &Encoder
{CompressionLevel
: NoCompression
}
88 if err
:= noenc
.Encode(&b2
, m
); err
!= nil {
92 if b2
.Len() <= b1
.Len() {
93 t
.Error("DefaultCompression encoding was larger than NoCompression encoding")
95 if _
, err
:= Decode(&b1
); err
!= nil {
96 t
.Error("cannot decode DefaultCompression")
98 if _
, err
:= Decode(&b2
); err
!= nil {
99 t
.Error("cannot decode NoCompression")
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})
110 m0
= m0
.SubImage(image
.Rect(50, 30, 250, 130)).(*image
.RGBA
)
111 m1
, err
:= encodeDecode(m0
)
123 func BenchmarkEncodeGray(b
*testing
.B
) {
125 img
:= image
.NewGray(image
.Rect(0, 0, 640, 480))
126 b
.SetBytes(640 * 480 * 1)
128 for i
:= 0; i
< b
.N
; i
++ {
129 Encode(ioutil
.Discard
, img
)
137 func (p
*pool
) Get() *EncoderBuffer
{
141 func (p
*pool
) Put(b
*EncoderBuffer
) {
145 func BenchmarkEncodeGrayWithBufferPool(b
*testing
.B
) {
147 img
:= image
.NewGray(image
.Rect(0, 0, 640, 480))
151 b
.SetBytes(640 * 480 * 1)
153 for i
:= 0; i
< b
.N
; i
++ {
154 e
.Encode(ioutil
.Discard
, img
)
158 func BenchmarkEncodeNRGBOpaque(b
*testing
.B
) {
160 img
:= image
.NewNRGBA(image
.Rect(0, 0, 640, 480))
161 // Set all pixels to 0xFF alpha to force opaque mode.
163 for y
:= bo
.Min
.Y
; y
< bo
.Max
.Y
; y
++ {
164 for x
:= bo
.Min
.X
; x
< bo
.Max
.X
; x
++ {
165 img
.Set(x
, y
, color
.NRGBA
{0, 0, 0, 255})
169 b
.Fatal("expected image to be opaque")
171 b
.SetBytes(640 * 480 * 4)
173 for i
:= 0; i
< b
.N
; i
++ {
174 Encode(ioutil
.Discard
, img
)
178 func BenchmarkEncodeNRGBA(b
*testing
.B
) {
180 img
:= image
.NewNRGBA(image
.Rect(0, 0, 640, 480))
182 b
.Fatal("expected image not to be opaque")
184 b
.SetBytes(640 * 480 * 4)
186 for i
:= 0; i
< b
.N
; i
++ {
187 Encode(ioutil
.Discard
, img
)
191 func BenchmarkEncodePaletted(b
*testing
.B
) {
193 img
:= image
.NewPaletted(image
.Rect(0, 0, 640, 480), color
.Palette
{
194 color
.RGBA
{0, 0, 0, 255},
195 color
.RGBA
{255, 255, 255, 255},
197 b
.SetBytes(640 * 480 * 1)
199 for i
:= 0; i
< b
.N
; i
++ {
200 Encode(ioutil
.Discard
, img
)
204 func BenchmarkEncodeRGBOpaque(b
*testing
.B
) {
206 img
:= image
.NewRGBA(image
.Rect(0, 0, 640, 480))
207 // Set all pixels to 0xFF alpha to force opaque mode.
209 for y
:= bo
.Min
.Y
; y
< bo
.Max
.Y
; y
++ {
210 for x
:= bo
.Min
.X
; x
< bo
.Max
.X
; x
++ {
211 img
.Set(x
, y
, color
.RGBA
{0, 0, 0, 255})
215 b
.Fatal("expected image to be opaque")
217 b
.SetBytes(640 * 480 * 4)
219 for i
:= 0; i
< b
.N
; i
++ {
220 Encode(ioutil
.Discard
, img
)
224 func BenchmarkEncodeRGBA(b
*testing
.B
) {
226 img
:= image
.NewRGBA(image
.Rect(0, 0, 640, 480))
228 b
.Fatal("expected image not to be opaque")
230 b
.SetBytes(640 * 480 * 4)
232 for i
:= 0; i
< b
.N
; i
++ {
233 Encode(ioutil
.Discard
, img
)