Skip to content

Commit

Permalink
short circuit rotat earlier (#1317)
Browse files Browse the repository at this point in the history
  • Loading branch information
hipstersmoothie authored Sep 2, 2024
1 parent 545c13a commit 0afb0a1
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions plugins/plugin-rotate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,12 @@ function matrixRotate<I extends JimpClass>(image: I, deg: number) {
throw new Error("Unsupported matrix rotation degree");
}

deg %= 360;

if (Math.abs(deg) === 0) {
// no rotation for 0, 360, -360, 720, -720, ...
return;
}

const w = image.bitmap.width;
const h = image.bitmap.height;

// decide which rotation angle to use
let angle;

switch (deg) {
// 90 degree & -270 degree are same
case 90:
Expand Down Expand Up @@ -126,7 +120,6 @@ function advancedRotate<I extends JimpClass>(
deg: number,
mode: boolean | ResizeStrategy
) {
deg %= 360;
const rad = (deg * Math.PI) / 180;
const cosine = Math.cos(rad);
const sine = Math.sin(rad);
Expand Down Expand Up @@ -235,8 +228,17 @@ export const methods = {
*/
rotate<I extends JimpClass>(image: I, options: RotateOptions) {
const parsed = RotateOptionsSchema.parse(options);
const { deg, mode = true } =
let { deg, mode = true } =
typeof parsed === "number" ? { deg: parsed } : parsed;

// No need to do extra rotation
deg %= 360;

// no rotation for 0, 360, -360, 720, -720, ...
if (deg % 360 === 0) {
return image;
}

// use matrixRotate if the angle is a multiple of 90 degrees (eg: 180 or -90) and resize is allowed or not needed.
const matrixRotateAllowed =
deg % 90 === 0 &&
Expand Down

0 comments on commit 0afb0a1

Please sign in to comment.