Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transform: reduce amount of calls to cos/sin #92

Merged
merged 1 commit into from
Jun 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions transform/rotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down