|
|
import { isNodePattern, throwError } from "@jimp/utils"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function matrixRotate(deg) { |
|
|
if (Math.abs(deg) % 90 !== 0) { |
|
|
throw new Error("Unsupported matrix rotation degree"); |
|
|
} |
|
|
|
|
|
deg %= 360; |
|
|
if (Math.abs(deg) === 0) { |
|
|
|
|
|
return; |
|
|
} |
|
|
|
|
|
const w = this.bitmap.width; |
|
|
const h = this.bitmap.height; |
|
|
|
|
|
|
|
|
let angle; |
|
|
switch (deg) { |
|
|
|
|
|
case 90: |
|
|
case -270: |
|
|
angle = 90; |
|
|
break; |
|
|
|
|
|
case 180: |
|
|
case -180: |
|
|
angle = 180; |
|
|
break; |
|
|
|
|
|
case 270: |
|
|
case -90: |
|
|
angle = -90; |
|
|
break; |
|
|
|
|
|
default: |
|
|
throw new Error("Unsupported matrix rotation degree"); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const nW = angle === 180 ? w : h; |
|
|
const nH = angle === 180 ? h : w; |
|
|
|
|
|
const dstBuffer = Buffer.alloc(this.bitmap.data.length); |
|
|
|
|
|
|
|
|
function createIdxTranslationFunction(w, h) { |
|
|
return function (x, y) { |
|
|
return (y * w + x) << 2; |
|
|
}; |
|
|
} |
|
|
|
|
|
const srcIdxFunction = createIdxTranslationFunction(w, h); |
|
|
const dstIdxFunction = createIdxTranslationFunction(nW, nH); |
|
|
|
|
|
for (let x = 0; x < w; x++) { |
|
|
for (let y = 0; y < h; y++) { |
|
|
const srcIdx = srcIdxFunction(x, y); |
|
|
const pixelRGBA = this.bitmap.data.readUInt32BE(srcIdx); |
|
|
|
|
|
let dstIdx; |
|
|
switch (angle) { |
|
|
case 90: |
|
|
dstIdx = dstIdxFunction(y, w - x - 1); |
|
|
break; |
|
|
case -90: |
|
|
dstIdx = dstIdxFunction(h - y - 1, x); |
|
|
break; |
|
|
case 180: |
|
|
dstIdx = dstIdxFunction(w - x - 1, h - y - 1); |
|
|
break; |
|
|
default: |
|
|
throw new Error("Unsupported matrix rotation angle"); |
|
|
} |
|
|
|
|
|
dstBuffer.writeUInt32BE(pixelRGBA, dstIdx); |
|
|
} |
|
|
} |
|
|
|
|
|
this.bitmap.data = dstBuffer; |
|
|
this.bitmap.width = nW; |
|
|
this.bitmap.height = nH; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function advancedRotate(deg, mode) { |
|
|
deg %= 360; |
|
|
const rad = (deg * Math.PI) / 180; |
|
|
const cosine = Math.cos(rad); |
|
|
const sine = Math.sin(rad); |
|
|
|
|
|
|
|
|
let w = this.bitmap.width; |
|
|
let h = this.bitmap.height; |
|
|
|
|
|
if (mode === true || typeof mode === "string") { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w = |
|
|
Math.ceil( |
|
|
Math.abs(this.bitmap.width * cosine) + |
|
|
Math.abs(this.bitmap.height * sine) |
|
|
) + 1; |
|
|
h = |
|
|
Math.ceil( |
|
|
Math.abs(this.bitmap.width * sine) + |
|
|
Math.abs(this.bitmap.height * cosine) |
|
|
) + 1; |
|
|
|
|
|
if (w % 2 !== 0) { |
|
|
w++; |
|
|
} |
|
|
|
|
|
if (h % 2 !== 0) { |
|
|
h++; |
|
|
} |
|
|
|
|
|
const c = this.cloneQuiet(); |
|
|
this.scanQuiet( |
|
|
0, |
|
|
0, |
|
|
this.bitmap.width, |
|
|
this.bitmap.height, |
|
|
function (x, y, idx) { |
|
|
this.bitmap.data.writeUInt32BE(this._background, idx); |
|
|
} |
|
|
); |
|
|
|
|
|
const max = Math.max(w, h, this.bitmap.width, this.bitmap.height); |
|
|
this.resize(max, max, mode); |
|
|
|
|
|
this.blit( |
|
|
c, |
|
|
this.bitmap.width / 2 - c.bitmap.width / 2, |
|
|
this.bitmap.height / 2 - c.bitmap.height / 2 |
|
|
); |
|
|
} |
|
|
|
|
|
const bW = this.bitmap.width; |
|
|
const bH = this.bitmap.height; |
|
|
const dstBuffer = Buffer.alloc(this.bitmap.data.length); |
|
|
|
|
|
function createTranslationFunction(deltaX, deltaY) { |
|
|
return function (x, y) { |
|
|
return { |
|
|
x: x + deltaX, |
|
|
y: y + deltaY, |
|
|
}; |
|
|
}; |
|
|
} |
|
|
|
|
|
const translate2Cartesian = createTranslationFunction(-(bW / 2), -(bH / 2)); |
|
|
const translate2Screen = createTranslationFunction( |
|
|
bW / 2 + 0.5, |
|
|
bH / 2 + 0.5 |
|
|
); |
|
|
|
|
|
for (let y = 1; y <= bH; y++) { |
|
|
for (let x = 1; x <= bW; x++) { |
|
|
const cartesian = translate2Cartesian(x, y); |
|
|
const source = translate2Screen( |
|
|
cosine * cartesian.x - sine * cartesian.y, |
|
|
cosine * cartesian.y + sine * cartesian.x |
|
|
); |
|
|
const dstIdx = (bW * (y - 1) + x - 1) << 2; |
|
|
|
|
|
if (source.x >= 0 && source.x < bW && source.y >= 0 && source.y < bH) { |
|
|
const srcIdx = ((bW * (source.y | 0) + source.x) | 0) << 2; |
|
|
const pixelRGBA = this.bitmap.data.readUInt32BE(srcIdx); |
|
|
dstBuffer.writeUInt32BE(pixelRGBA, dstIdx); |
|
|
} else { |
|
|
|
|
|
dstBuffer.writeUInt32BE(this._background, dstIdx); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
this.bitmap.data = dstBuffer; |
|
|
|
|
|
if (mode === true || typeof mode === "string") { |
|
|
|
|
|
const x = bW / 2 - w / 2; |
|
|
const y = bH / 2 - h / 2; |
|
|
this.crop(x, y, w, h); |
|
|
} |
|
|
} |
|
|
|
|
|
export default () => ({ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rotate(deg, mode, cb) { |
|
|
|
|
|
if (typeof mode === "undefined" || mode === null) { |
|
|
|
|
|
|
|
|
|
|
|
mode = true; |
|
|
} |
|
|
|
|
|
if (typeof mode === "function" && typeof cb === "undefined") { |
|
|
|
|
|
cb = mode; |
|
|
mode = true; |
|
|
} |
|
|
|
|
|
if (typeof deg !== "number") { |
|
|
return throwError.call(this, "deg must be a number", cb); |
|
|
} |
|
|
|
|
|
if (typeof mode !== "boolean" && typeof mode !== "string") { |
|
|
return throwError.call(this, "mode must be a boolean or a string", cb); |
|
|
} |
|
|
|
|
|
|
|
|
const matrixRotateAllowed = |
|
|
deg % 90 === 0 && |
|
|
(mode || this.bitmap.width === this.bitmap.height || deg % 180 === 0); |
|
|
|
|
|
if (matrixRotateAllowed) { |
|
|
matrixRotate.call(this, deg); |
|
|
} else { |
|
|
advancedRotate.call(this, deg, mode, cb); |
|
|
} |
|
|
|
|
|
if (isNodePattern(cb)) { |
|
|
cb.call(this, null, this); |
|
|
} |
|
|
|
|
|
return this; |
|
|
}, |
|
|
}); |
|
|
|