aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNigel Tao <nigeltao@golang.org>2015-07-03 11:34:18 +1000
committerNigel Tao <nigeltao@golang.org>2015-07-03 03:08:40 +0000
commit40a1516a098b9843e2d3e5ee2b9471a57d6f2470 (patch)
tree50bb4f3fcb357825bb9e290aed8087c9872ab3a8
parent09b5463d9bcdd7b9562838e0b44029b6c04c487d (diff)
downloadgo-40a1516a098b9843e2d3e5ee2b9471a57d6f2470.tar.gz
go-40a1516a098b9843e2d3e5ee2b9471a57d6f2470.zip
image/draw: fix double-draw when the dst is paletted.
The second (fallback) draw is a no-op, but it's a non-trivial amount of work. Fixes #11550. benchmark old ns/op new ns/op delta BenchmarkPaletted-4 16301219 7309568 -55.16% Change-Id: Ic88c537b2b0c710cf517888f3dd15cb702dd142f Reviewed-on: https://go-review.googlesource.com/11858 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
-rw-r--r--src/image/draw/bench_test.go24
-rw-r--r--src/image/draw/draw.go1
2 files changed, 24 insertions, 1 deletions
diff --git a/src/image/draw/bench_test.go b/src/image/draw/bench_test.go
index 51145d1127..7b89f95d11 100644
--- a/src/image/draw/bench_test.go
+++ b/src/image/draw/bench_test.go
@@ -7,6 +7,7 @@ package draw
import (
"image"
"image/color"
+ "reflect"
"testing"
)
@@ -15,6 +16,11 @@ const (
srcw, srch = 400, 300
)
+var palette = color.Palette{
+ color.Black,
+ color.White,
+}
+
// bench benchmarks drawing src and mask images onto a dst image with the
// given op and the color models to create those images from.
// The created images' pixels are initialized to non-zero values.
@@ -50,7 +56,19 @@ func bench(b *testing.B, dcm, scm, mcm color.Model, op Op) {
}
dst = dst1
default:
- b.Fatal("unknown destination color model", dcm)
+ // The == operator isn't defined on a color.Palette (a slice), so we
+ // use reflection.
+ if reflect.DeepEqual(dcm, palette) {
+ dst1 := image.NewPaletted(image.Rect(0, 0, dstw, dsth), palette)
+ for y := 0; y < dsth; y++ {
+ for x := 0; x < dstw; x++ {
+ dst1.SetColorIndex(x, y, uint8(x^y)&1)
+ }
+ }
+ dst = dst1
+ } else {
+ b.Fatal("unknown destination color model", dcm)
+ }
}
var src image.Image
@@ -218,6 +236,10 @@ func BenchmarkRGBA(b *testing.B) {
bench(b, color.RGBAModel, color.RGBA64Model, nil, Src)
}
+func BenchmarkPaletted(b *testing.B) {
+ bench(b, palette, color.RGBAModel, nil, Src)
+}
+
// The BenchmarkGenericFoo functions exercise the generic, slow-path code.
func BenchmarkGenericOver(b *testing.B) {
diff --git a/src/image/draw/draw.go b/src/image/draw/draw.go
index ee1126cd08..3087c07ff5 100644
--- a/src/image/draw/draw.go
+++ b/src/image/draw/draw.go
@@ -176,6 +176,7 @@ func DrawMask(dst Image, r image.Rectangle, src image.Image, sp image.Point, mas
case *image.Paletted:
if op == Src && mask == nil && !processBackward(dst, r, src, sp) {
drawPaletted(dst0, r, src, sp, false)
+ return
}
}