Skip to content

Commit 6c76ede

Browse files
committed
image/gif: have BenchmarkEncodeRealisticRGBA convert to RGBA
Change-Id: I98f5d987b92a29dcff06ae23b92f293cc7d6c02f Reviewed-on: https://go-review.googlesource.com/c/go/+/252597 Reviewed-by: David Symonds <[email protected]>
1 parent a4171d8 commit 6c76ede

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/image/gif/writer_test.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -658,27 +658,27 @@ func TestEncodeWrappedImage(t *testing.T) {
658658
}
659659

660660
func BenchmarkEncodeRandomPaletted(b *testing.B) {
661-
img := image.NewPaletted(image.Rect(0, 0, 640, 480), palette.Plan9)
661+
paletted := image.NewPaletted(image.Rect(0, 0, 640, 480), palette.Plan9)
662662
rnd := rand.New(rand.NewSource(123))
663-
for i := range img.Pix {
664-
img.Pix[i] = uint8(rnd.Intn(256))
663+
for i := range paletted.Pix {
664+
paletted.Pix[i] = uint8(rnd.Intn(256))
665665
}
666666

667667
b.SetBytes(640 * 480 * 1)
668668
b.ReportAllocs()
669669
b.ResetTimer()
670670
for i := 0; i < b.N; i++ {
671-
Encode(ioutil.Discard, img, nil)
671+
Encode(ioutil.Discard, paletted, nil)
672672
}
673673
}
674674

675675
func BenchmarkEncodeRandomRGBA(b *testing.B) {
676-
img := image.NewRGBA(image.Rect(0, 0, 640, 480))
677-
bo := img.Bounds()
676+
rgba := image.NewRGBA(image.Rect(0, 0, 640, 480))
677+
bo := rgba.Bounds()
678678
rnd := rand.New(rand.NewSource(123))
679679
for y := bo.Min.Y; y < bo.Max.Y; y++ {
680680
for x := bo.Min.X; x < bo.Max.X; x++ {
681-
img.SetRGBA(x, y, color.RGBA{
681+
rgba.SetRGBA(x, y, color.RGBA{
682682
uint8(rnd.Intn(256)),
683683
uint8(rnd.Intn(256)),
684684
uint8(rnd.Intn(256)),
@@ -691,24 +691,24 @@ func BenchmarkEncodeRandomRGBA(b *testing.B) {
691691
b.ReportAllocs()
692692
b.ResetTimer()
693693
for i := 0; i < b.N; i++ {
694-
Encode(ioutil.Discard, img, nil)
694+
Encode(ioutil.Discard, rgba, nil)
695695
}
696696
}
697697

698698
func BenchmarkEncodeRealisticPaletted(b *testing.B) {
699-
rgba, err := readImg("../testdata/video-001.png")
699+
img, err := readImg("../testdata/video-001.png")
700700
if err != nil {
701701
b.Fatalf("readImg: %v", err)
702702
}
703-
bo := rgba.Bounds()
704-
img := image.NewPaletted(bo, palette.Plan9)
705-
draw.Draw(img, bo, rgba, bo.Min, draw.Src)
703+
bo := img.Bounds()
704+
paletted := image.NewPaletted(bo, palette.Plan9)
705+
draw.Draw(paletted, bo, img, bo.Min, draw.Src)
706706

707707
b.SetBytes(int64(bo.Dx() * bo.Dy() * 1))
708708
b.ReportAllocs()
709709
b.ResetTimer()
710710
for i := 0; i < b.N; i++ {
711-
Encode(ioutil.Discard, img, nil)
711+
Encode(ioutil.Discard, paletted, nil)
712712
}
713713
}
714714

@@ -718,11 +718,17 @@ func BenchmarkEncodeRealisticRGBA(b *testing.B) {
718718
b.Fatalf("readImg: %v", err)
719719
}
720720
bo := img.Bounds()
721+
// Converting img to rgba is redundant for video-001.png, which is already
722+
// in the RGBA format, but for those copy/pasting this benchmark (but
723+
// changing the source image), the conversion ensures that we're still
724+
// benchmarking encoding an RGBA image.
725+
rgba := image.NewRGBA(bo)
726+
draw.Draw(rgba, bo, img, bo.Min, draw.Src)
721727

722728
b.SetBytes(int64(bo.Dx() * bo.Dy() * 4))
723729
b.ReportAllocs()
724730
b.ResetTimer()
725731
for i := 0; i < b.N; i++ {
726-
Encode(ioutil.Discard, img, nil)
732+
Encode(ioutil.Discard, rgba, nil)
727733
}
728734
}

0 commit comments

Comments
 (0)