@@ -658,27 +658,27 @@ func TestEncodeWrappedImage(t *testing.T) {
658
658
}
659
659
660
660
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 )
662
662
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 ))
665
665
}
666
666
667
667
b .SetBytes (640 * 480 * 1 )
668
668
b .ReportAllocs ()
669
669
b .ResetTimer ()
670
670
for i := 0 ; i < b .N ; i ++ {
671
- Encode (ioutil .Discard , img , nil )
671
+ Encode (ioutil .Discard , paletted , nil )
672
672
}
673
673
}
674
674
675
675
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 ()
678
678
rnd := rand .New (rand .NewSource (123 ))
679
679
for y := bo .Min .Y ; y < bo .Max .Y ; y ++ {
680
680
for x := bo .Min .X ; x < bo .Max .X ; x ++ {
681
- img .SetRGBA (x , y , color.RGBA {
681
+ rgba .SetRGBA (x , y , color.RGBA {
682
682
uint8 (rnd .Intn (256 )),
683
683
uint8 (rnd .Intn (256 )),
684
684
uint8 (rnd .Intn (256 )),
@@ -691,24 +691,24 @@ func BenchmarkEncodeRandomRGBA(b *testing.B) {
691
691
b .ReportAllocs ()
692
692
b .ResetTimer ()
693
693
for i := 0 ; i < b .N ; i ++ {
694
- Encode (ioutil .Discard , img , nil )
694
+ Encode (ioutil .Discard , rgba , nil )
695
695
}
696
696
}
697
697
698
698
func BenchmarkEncodeRealisticPaletted (b * testing.B ) {
699
- rgba , err := readImg ("../testdata/video-001.png" )
699
+ img , err := readImg ("../testdata/video-001.png" )
700
700
if err != nil {
701
701
b .Fatalf ("readImg: %v" , err )
702
702
}
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 )
706
706
707
707
b .SetBytes (int64 (bo .Dx () * bo .Dy () * 1 ))
708
708
b .ReportAllocs ()
709
709
b .ResetTimer ()
710
710
for i := 0 ; i < b .N ; i ++ {
711
- Encode (ioutil .Discard , img , nil )
711
+ Encode (ioutil .Discard , paletted , nil )
712
712
}
713
713
}
714
714
@@ -718,11 +718,17 @@ func BenchmarkEncodeRealisticRGBA(b *testing.B) {
718
718
b .Fatalf ("readImg: %v" , err )
719
719
}
720
720
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 )
721
727
722
728
b .SetBytes (int64 (bo .Dx () * bo .Dy () * 4 ))
723
729
b .ReportAllocs ()
724
730
b .ResetTimer ()
725
731
for i := 0 ; i < b .N ; i ++ {
726
- Encode (ioutil .Discard , img , nil )
732
+ Encode (ioutil .Discard , rgba , nil )
727
733
}
728
734
}
0 commit comments