Skip to content

Commit c62f9cd

Browse files
Merge pull request #92 from sbinet-staging/rotate-perf
2 parents 164bd2c + 1b6d543 commit c62f9cd

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

transform/rotate.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,16 @@ func Rotate(img image.Image, angle float64, options *RotationOptions) *image.RGB
6666
angleRadians := -angle * (math.Pi / 180)
6767

6868
var dstW, dstH int
69+
var sin, cos = math.Sincos(angleRadians)
6970
if resizeBounds {
7071
// Reserve larger size in destination image for full image bounds rotation
7172
// If not preserving size, always take image center as pivot
7273
pivotX, pivotY = float64(srcW)/2, float64(srcH)/2
7374

74-
a := math.Abs(float64(srcW) * math.Sin(angleRadians))
75-
b := math.Abs(float64(srcW) * math.Cos(angleRadians))
76-
c := math.Abs(float64(srcH) * math.Sin(angleRadians))
77-
d := math.Abs(float64(srcH) * math.Cos(angleRadians))
75+
a := math.Abs(float64(srcW) * sin)
76+
b := math.Abs(float64(srcW) * cos)
77+
c := math.Abs(float64(srcH) * sin)
78+
d := math.Abs(float64(srcH) * cos)
7879

7980
dstW, dstH = int(c+b+0.5), int(a+d+0.5)
8081
} else {
@@ -100,8 +101,8 @@ func Rotate(img image.Image, angle float64, options *RotationOptions) *image.RGB
100101
for x := xStart; x < xEnd; x++ {
101102
dx := float64(x) - pivotX + 0.5
102103

103-
ix := int((math.Cos(angleRadians)*dx - math.Sin(angleRadians)*dy + pivotX))
104-
iy := int((math.Sin(angleRadians)*dx + math.Cos(angleRadians)*dy + pivotY))
104+
ix := int((cos*dx - sin*dy + pivotX))
105+
iy := int((sin*dx + cos*dy + pivotY))
105106

106107
if ix < 0 || ix >= srcW || iy < 0 || iy >= srcH {
107108
continue

0 commit comments

Comments
 (0)