Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@jimp/plugin-rotate/es/index.js
1126 views
1
"use strict";
2
3
Object.defineProperty(exports, "__esModule", {
4
value: true
5
});
6
exports["default"] = void 0;
7
8
var _utils = require("@jimp/utils");
9
10
/**
11
* Rotates an image clockwise by an arbitrary number of degrees. NB: 'this' must be a Jimp object.
12
* @param {number} deg the number of degrees to rotate the image by
13
* @param {string|boolean} mode (optional) resize mode or a boolean, if false then the width and height of the image will not be changed
14
*/
15
function advancedRotate(deg, mode) {
16
deg %= 360;
17
var rad = deg * Math.PI / 180;
18
var cosine = Math.cos(rad);
19
var sine = Math.sin(rad); // the final width and height will change if resize == true
20
21
var w = this.bitmap.width;
22
var h = this.bitmap.height;
23
24
if (mode === true || typeof mode === 'string') {
25
// resize the image to it maximum dimension and blit the existing image
26
// onto the center so that when it is rotated the image is kept in bounds
27
// http://stackoverflow.com/questions/3231176/how-to-get-size-of-a-rotated-rectangle
28
// Plus 1 border pixel to ensure to show all rotated result for some cases.
29
w = Math.ceil(Math.abs(this.bitmap.width * cosine) + Math.abs(this.bitmap.height * sine)) + 1;
30
h = 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.
31
32
if (w % 2 !== 0) {
33
w++;
34
}
35
36
if (h % 2 !== 0) {
37
h++;
38
}
39
40
var c = this.cloneQuiet();
41
this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, function (x, y, idx) {
42
this.bitmap.data.writeUInt32BE(this._background, idx);
43
});
44
var max = Math.max(w, h, this.bitmap.width, this.bitmap.height);
45
this.resize(max, max, mode);
46
this.blit(c, this.bitmap.width / 2 - c.bitmap.width / 2, this.bitmap.height / 2 - c.bitmap.height / 2);
47
}
48
49
var bW = this.bitmap.width;
50
var bH = this.bitmap.height;
51
var dstBuffer = Buffer.alloc(this.bitmap.data.length);
52
53
function createTranslationFunction(deltaX, deltaY) {
54
return function (x, y) {
55
return {
56
x: x + deltaX,
57
y: y + deltaY
58
};
59
};
60
}
61
62
var translate2Cartesian = createTranslationFunction(-(bW / 2), -(bH / 2));
63
var translate2Screen = createTranslationFunction(bW / 2 + 0.5, bH / 2 + 0.5);
64
65
for (var y = 1; y <= bH; y++) {
66
for (var x = 1; x <= bW; x++) {
67
var cartesian = translate2Cartesian(x, y);
68
var source = translate2Screen(cosine * cartesian.x - sine * cartesian.y, cosine * cartesian.y + sine * cartesian.x);
69
var dstIdx = bW * (y - 1) + x - 1 << 2;
70
71
if (source.x >= 0 && source.x < bW && source.y >= 0 && source.y < bH) {
72
var srcIdx = (bW * (source.y | 0) + source.x | 0) << 2;
73
var pixelRGBA = this.bitmap.data.readUInt32BE(srcIdx);
74
dstBuffer.writeUInt32BE(pixelRGBA, dstIdx);
75
} else {
76
// reset off-image pixels
77
dstBuffer.writeUInt32BE(this._background, dstIdx);
78
}
79
}
80
}
81
82
this.bitmap.data = dstBuffer;
83
84
if (mode === true || typeof mode === 'string') {
85
// now crop the image to the final size
86
var _x = bW / 2 - w / 2;
87
88
var _y = bH / 2 - h / 2;
89
90
this.crop(_x, _y, w, h);
91
}
92
}
93
94
var _default = function _default() {
95
return {
96
/**
97
* Rotates the image clockwise by a number of degrees. By default the width and height of the image will be resized appropriately.
98
* @param {number} deg the number of degrees to rotate the image by
99
* @param {string|boolean} mode (optional) resize mode or a boolean, if false then the width and height of the image will not be changed
100
* @param {function(Error, Jimp)} cb (optional) a callback for when complete
101
* @returns {Jimp} this for chaining of methods
102
*/
103
rotate: function rotate(deg, mode, cb) {
104
// enable overloading
105
if (typeof mode === 'undefined' || mode === null) {
106
// e.g. image.resize(120);
107
// e.g. image.resize(120, null, cb);
108
// e.g. image.resize(120, undefined, cb);
109
mode = true;
110
}
111
112
if (typeof mode === 'function' && typeof cb === 'undefined') {
113
// e.g. image.resize(120, cb);
114
cb = mode;
115
mode = true;
116
}
117
118
if (typeof deg !== 'number') {
119
return _utils.throwError.call(this, 'deg must be a number', cb);
120
}
121
122
if (typeof mode !== 'boolean' && typeof mode !== 'string') {
123
return _utils.throwError.call(this, 'mode must be a boolean or a string', cb);
124
}
125
126
advancedRotate.call(this, deg, mode, cb);
127
128
if ((0, _utils.isNodePattern)(cb)) {
129
cb.call(this, null, this);
130
}
131
132
return this;
133
}
134
};
135
};
136
137
exports["default"] = _default;
138
//# sourceMappingURL=index.js.map
139