Path: blob/master/node_modules/@jimp/plugin-rotate/src/index.js
1129 views
import { throwError, isNodePattern } from '@jimp/utils';12/**3* Rotates an image clockwise by an arbitrary number of degrees. NB: 'this' must be a Jimp object.4* @param {number} deg the number of degrees to rotate the image by5* @param {string|boolean} mode (optional) resize mode or a boolean, if false then the width and height of the image will not be changed6*/7function advancedRotate(deg, mode) {8deg %= 360;9const rad = (deg * Math.PI) / 180;10const cosine = Math.cos(rad);11const sine = Math.sin(rad);1213// the final width and height will change if resize == true14let w = this.bitmap.width;15let h = this.bitmap.height;1617if (mode === true || typeof mode === 'string') {18// resize the image to it maximum dimension and blit the existing image19// onto the center so that when it is rotated the image is kept in bounds2021// http://stackoverflow.com/questions/3231176/how-to-get-size-of-a-rotated-rectangle22// Plus 1 border pixel to ensure to show all rotated result for some cases.23w =24Math.ceil(25Math.abs(this.bitmap.width * cosine) +26Math.abs(this.bitmap.height * sine)27) + 1;28h =29Math.ceil(30Math.abs(this.bitmap.width * sine) +31Math.abs(this.bitmap.height * cosine)32) + 1;33// Ensure destination to have even size to a better result.34if (w % 2 !== 0) {35w++;36}3738if (h % 2 !== 0) {39h++;40}4142const c = this.cloneQuiet();43this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, function(44x,45y,46idx47) {48this.bitmap.data.writeUInt32BE(this._background, idx);49});5051const max = Math.max(w, h, this.bitmap.width, this.bitmap.height);52this.resize(max, max, mode);5354this.blit(55c,56this.bitmap.width / 2 - c.bitmap.width / 2,57this.bitmap.height / 2 - c.bitmap.height / 258);59}6061const bW = this.bitmap.width;62const bH = this.bitmap.height;63const dstBuffer = Buffer.alloc(this.bitmap.data.length);6465function createTranslationFunction(deltaX, deltaY) {66return function(x, y) {67return {68x: x + deltaX,69y: y + deltaY70};71};72}7374const translate2Cartesian = createTranslationFunction(-(bW / 2), -(bH / 2));75const translate2Screen = createTranslationFunction(76bW / 2 + 0.5,77bH / 2 + 0.578);7980for (let y = 1; y <= bH; y++) {81for (let x = 1; x <= bW; x++) {82const cartesian = translate2Cartesian(x, y);83const source = translate2Screen(84cosine * cartesian.x - sine * cartesian.y,85cosine * cartesian.y + sine * cartesian.x86);87const dstIdx = (bW * (y - 1) + x - 1) << 2;8889if (source.x >= 0 && source.x < bW && source.y >= 0 && source.y < bH) {90const srcIdx = ((bW * (source.y | 0) + source.x) | 0) << 2;91const pixelRGBA = this.bitmap.data.readUInt32BE(srcIdx);92dstBuffer.writeUInt32BE(pixelRGBA, dstIdx);93} else {94// reset off-image pixels95dstBuffer.writeUInt32BE(this._background, dstIdx);96}97}98}99100this.bitmap.data = dstBuffer;101102if (mode === true || typeof mode === 'string') {103// now crop the image to the final size104const x = bW / 2 - w / 2;105const y = bH / 2 - h / 2;106this.crop(x, y, w, h);107}108}109110export default () => ({111/**112* Rotates the image clockwise by a number of degrees. By default the width and height of the image will be resized appropriately.113* @param {number} deg the number of degrees to rotate the image by114* @param {string|boolean} mode (optional) resize mode or a boolean, if false then the width and height of the image will not be changed115* @param {function(Error, Jimp)} cb (optional) a callback for when complete116* @returns {Jimp} this for chaining of methods117*/118rotate(deg, mode, cb) {119// enable overloading120if (typeof mode === 'undefined' || mode === null) {121// e.g. image.resize(120);122// e.g. image.resize(120, null, cb);123// e.g. image.resize(120, undefined, cb);124mode = true;125}126127if (typeof mode === 'function' && typeof cb === 'undefined') {128// e.g. image.resize(120, cb);129cb = mode;130mode = true;131}132133if (typeof deg !== 'number') {134return throwError.call(this, 'deg must be a number', cb);135}136137if (typeof mode !== 'boolean' && typeof mode !== 'string') {138return throwError.call(this, 'mode must be a boolean or a string', cb);139}140141advancedRotate.call(this, deg, mode, cb);142143if (isNodePattern(cb)) {144cb.call(this, null, this);145}146147return this;148}149});150151152