From 20842979997c90fee2ef3417854617dc5e1e621c Mon Sep 17 00:00:00 2001 From: George Stark Date: Tue, 30 Mar 2021 13:30:23 +0300 Subject: [PATCH] add method to change drawing interpolator default interpolator used for drawing text and images is draw.BiLinear and it's rather slow against e.g. draw.NearestNeighbor The difference is significant especially on weak hardware e.g. on raspberry pi zero (ARMv6) drawing short string can be 10-16 times faster with draw.NearestNeighbor Signed-off-by: George Stark --- context.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/context.go b/context.go index 1ddb09f..5cb7ddc 100644 --- a/context.go +++ b/context.go @@ -77,6 +77,7 @@ type Context struct { fontHeight float64 matrix Matrix stack []*Context + interpolator draw.Interpolator } // NewContext creates a new image.RGBA with the specified width and height @@ -109,9 +110,14 @@ func NewContextForRGBA(im *image.RGBA) *Context { fontFace: basicfont.Face7x13, fontHeight: 13, matrix: Identity(), + interpolator: draw.BiLinear, } } +func (dc *Context) SetInterpolator(interpolator draw.Interpolator) { + dc.interpolator = interpolator +} + // GetCurrentPoint will return the current point and if there is a current point. // The point will have been transformed by the context's transformation matrix. func (dc *Context) GetCurrentPoint() (Point, bool) { @@ -673,7 +679,7 @@ func (dc *Context) DrawImageAnchored(im image.Image, x, y int, ax, ay float64) { s := im.Bounds().Size() x -= int(ax * float64(s.X)) y -= int(ay * float64(s.Y)) - transformer := draw.BiLinear + transformer := dc.interpolator fx, fy := float64(x), float64(y) m := dc.matrix.Translate(fx, fy) s2d := f64.Aff3{m.XX, m.XY, m.X0, m.YX, m.YY, m.Y0} @@ -728,7 +734,7 @@ func (dc *Context) drawString(im *image.RGBA, s string, x, y float64) { continue } sr := dr.Sub(dr.Min) - transformer := draw.BiLinear + transformer := dc.interpolator fx, fy := float64(dr.Min.X), float64(dr.Min.Y) m := dc.matrix.Translate(fx, fy) s2d := f64.Aff3{m.XX, m.XY, m.X0, m.YX, m.YY, m.Y0}