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