From 1b6d5431fcb5909799e68242043229fdb838face Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Tue, 14 Jun 2022 15:42:53 +0200 Subject: [PATCH] transform: reduce amount of calls to cos/sin Signed-off-by: Sebastien Binet --- transform/rotate.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/transform/rotate.go b/transform/rotate.go index d716212..7bcbb1b 100644 --- a/transform/rotate.go +++ b/transform/rotate.go @@ -66,15 +66,16 @@ func Rotate(img image.Image, angle float64, options *RotationOptions) *image.RGB angleRadians := -angle * (math.Pi / 180) var dstW, dstH int + var sin, cos = math.Sincos(angleRadians) if resizeBounds { // Reserve larger size in destination image for full image bounds rotation // If not preserving size, always take image center as pivot pivotX, pivotY = float64(srcW)/2, float64(srcH)/2 - a := math.Abs(float64(srcW) * math.Sin(angleRadians)) - b := math.Abs(float64(srcW) * math.Cos(angleRadians)) - c := math.Abs(float64(srcH) * math.Sin(angleRadians)) - d := math.Abs(float64(srcH) * math.Cos(angleRadians)) + a := math.Abs(float64(srcW) * sin) + b := math.Abs(float64(srcW) * cos) + c := math.Abs(float64(srcH) * sin) + d := math.Abs(float64(srcH) * cos) dstW, dstH = int(c+b+0.5), int(a+d+0.5) } else { @@ -100,8 +101,8 @@ func Rotate(img image.Image, angle float64, options *RotationOptions) *image.RGB for x := xStart; x < xEnd; x++ { dx := float64(x) - pivotX + 0.5 - ix := int((math.Cos(angleRadians)*dx - math.Sin(angleRadians)*dy + pivotX)) - iy := int((math.Sin(angleRadians)*dx + math.Cos(angleRadians)*dy + pivotY)) + ix := int((cos*dx - sin*dy + pivotX)) + iy := int((sin*dx + cos*dy + pivotY)) if ix < 0 || ix >= srcW || iy < 0 || iy >= srcH { continue