Path: blob/master/node_modules/@jimp/plugin-rotate/es/index.js
1126 views
"use strict";12Object.defineProperty(exports, "__esModule", {3value: true4});5exports["default"] = void 0;67var _utils = require("@jimp/utils");89/**10* Rotates an image clockwise by an arbitrary number of degrees. NB: 'this' must be a Jimp object.11* @param {number} deg the number of degrees to rotate the image by12* @param {string|boolean} mode (optional) resize mode or a boolean, if false then the width and height of the image will not be changed13*/14function advancedRotate(deg, mode) {15deg %= 360;16var rad = deg * Math.PI / 180;17var cosine = Math.cos(rad);18var sine = Math.sin(rad); // the final width and height will change if resize == true1920var w = this.bitmap.width;21var h = this.bitmap.height;2223if (mode === true || typeof mode === 'string') {24// resize the image to it maximum dimension and blit the existing image25// onto the center so that when it is rotated the image is kept in bounds26// http://stackoverflow.com/questions/3231176/how-to-get-size-of-a-rotated-rectangle27// Plus 1 border pixel to ensure to show all rotated result for some cases.28w = Math.ceil(Math.abs(this.bitmap.width * cosine) + Math.abs(this.bitmap.height * sine)) + 1;29h = Math.ceil(Math.abs(this.bitmap.width * sine) + Math.abs(this.bitmap.height * cosine)) + 1; // Ensure destination to have even size to a better result.3031if (w % 2 !== 0) {32w++;33}3435if (h % 2 !== 0) {36h++;37}3839var c = this.cloneQuiet();40this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, function (x, y, idx) {41this.bitmap.data.writeUInt32BE(this._background, idx);42});43var max = Math.max(w, h, this.bitmap.width, this.bitmap.height);44this.resize(max, max, mode);45this.blit(c, this.bitmap.width / 2 - c.bitmap.width / 2, this.bitmap.height / 2 - c.bitmap.height / 2);46}4748var bW = this.bitmap.width;49var bH = this.bitmap.height;50var dstBuffer = Buffer.alloc(this.bitmap.data.length);5152function createTranslationFunction(deltaX, deltaY) {53return function (x, y) {54return {55x: x + deltaX,56y: y + deltaY57};58};59}6061var translate2Cartesian = createTranslationFunction(-(bW / 2), -(bH / 2));62var translate2Screen = createTranslationFunction(bW / 2 + 0.5, bH / 2 + 0.5);6364for (var y = 1; y <= bH; y++) {65for (var x = 1; x <= bW; x++) {66var cartesian = translate2Cartesian(x, y);67var source = translate2Screen(cosine * cartesian.x - sine * cartesian.y, cosine * cartesian.y + sine * cartesian.x);68var dstIdx = bW * (y - 1) + x - 1 << 2;6970if (source.x >= 0 && source.x < bW && source.y >= 0 && source.y < bH) {71var srcIdx = (bW * (source.y | 0) + source.x | 0) << 2;72var pixelRGBA = this.bitmap.data.readUInt32BE(srcIdx);73dstBuffer.writeUInt32BE(pixelRGBA, dstIdx);74} else {75// reset off-image pixels76dstBuffer.writeUInt32BE(this._background, dstIdx);77}78}79}8081this.bitmap.data = dstBuffer;8283if (mode === true || typeof mode === 'string') {84// now crop the image to the final size85var _x = bW / 2 - w / 2;8687var _y = bH / 2 - h / 2;8889this.crop(_x, _y, w, h);90}91}9293var _default = function _default() {94return {95/**96* Rotates the image clockwise by a number of degrees. By default the width and height of the image will be resized appropriately.97* @param {number} deg the number of degrees to rotate the image by98* @param {string|boolean} mode (optional) resize mode or a boolean, if false then the width and height of the image will not be changed99* @param {function(Error, Jimp)} cb (optional) a callback for when complete100* @returns {Jimp} this for chaining of methods101*/102rotate: function rotate(deg, mode, cb) {103// enable overloading104if (typeof mode === 'undefined' || mode === null) {105// e.g. image.resize(120);106// e.g. image.resize(120, null, cb);107// e.g. image.resize(120, undefined, cb);108mode = true;109}110111if (typeof mode === 'function' && typeof cb === 'undefined') {112// e.g. image.resize(120, cb);113cb = mode;114mode = true;115}116117if (typeof deg !== 'number') {118return _utils.throwError.call(this, 'deg must be a number', cb);119}120121if (typeof mode !== 'boolean' && typeof mode !== 'string') {122return _utils.throwError.call(this, 'mode must be a boolean or a string', cb);123}124125advancedRotate.call(this, deg, mode, cb);126127if ((0, _utils.isNodePattern)(cb)) {128cb.call(this, null, this);129}130131return this;132}133};134};135136exports["default"] = _default;137//# sourceMappingURL=index.js.map138139