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 <stark.georgy@gmail.com>
This commit is contained in:
George Stark 2021-03-30 13:30:23 +03:00
parent af4cd58078
commit 2084297999

View File

@ -77,6 +77,7 @@ type Context struct {
fontHeight float64 fontHeight float64
matrix Matrix matrix Matrix
stack []*Context stack []*Context
interpolator draw.Interpolator
} }
// NewContext creates a new image.RGBA with the specified width and height // 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, fontFace: basicfont.Face7x13,
fontHeight: 13, fontHeight: 13,
matrix: Identity(), 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. // 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. // The point will have been transformed by the context's transformation matrix.
func (dc *Context) GetCurrentPoint() (Point, bool) { 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() s := im.Bounds().Size()
x -= int(ax * float64(s.X)) x -= int(ax * float64(s.X))
y -= int(ay * float64(s.Y)) y -= int(ay * float64(s.Y))
transformer := draw.BiLinear transformer := dc.interpolator
fx, fy := float64(x), float64(y) fx, fy := float64(x), float64(y)
m := dc.matrix.Translate(fx, fy) m := dc.matrix.Translate(fx, fy)
s2d := f64.Aff3{m.XX, m.XY, m.X0, m.YX, m.YY, m.Y0} 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 continue
} }
sr := dr.Sub(dr.Min) sr := dr.Sub(dr.Min)
transformer := draw.BiLinear transformer := dc.interpolator
fx, fy := float64(dr.Min.X), float64(dr.Min.Y) fx, fy := float64(dr.Min.X), float64(dr.Min.Y)
m := dc.matrix.Translate(fx, fy) m := dc.matrix.Translate(fx, fy)
s2d := f64.Aff3{m.XX, m.XY, m.X0, m.YX, m.YY, m.Y0} s2d := f64.Aff3{m.XX, m.XY, m.X0, m.YX, m.YY, m.Y0}