MCPcopy
hub / github.com/jimp-dev/jimp / advancedRotate

Function advancedRotate

plugins/plugin-rotate/src/index.ts:118–215  ·  view source on GitHub ↗

* Rotates an image counter-clockwise by an arbitrary number of degrees. NB: 'this' must be a Jimp object. * @param {number} deg the number of degrees to rotate the image by

(
  image: I,
  deg: number,
  mode: boolean | ResizeStrategy
)

Source from the content-addressed store, hash-verified

116 * @param {number} deg the number of degrees to rotate the image by
117 */
118function advancedRotate<I extends JimpClass>(
119 image: I,
120 deg: number,
121 mode: boolean | ResizeStrategy
122) {
123 const rad = (deg * Math.PI) / 180;
124 const cosine = Math.cos(rad);
125 const sine = Math.sin(rad);
126
127 // the final width and height will change if resize == true
128 let w = image.bitmap.width;
129 let h = image.bitmap.height;
130
131 if (mode === true || typeof mode === "string") {
132 // resize the image to it maximum dimension and blit the existing image
133 // onto the center so that when it is rotated the image is kept in bounds
134
135 // http://stackoverflow.com/questions/3231176/how-to-get-size-of-a-rotated-rectangle
136 // Plus 1 border pixel to ensure to show all rotated result for some cases.
137 w =
138 Math.ceil(
139 Math.abs(image.bitmap.width * cosine) +
140 Math.abs(image.bitmap.height * sine)
141 ) + 1;
142 h =
143 Math.ceil(
144 Math.abs(image.bitmap.width * sine) +
145 Math.abs(image.bitmap.height * cosine)
146 ) + 1;
147 // Ensure destination to have even size to a better result.
148 if (w % 2 !== 0) {
149 w++;
150 }
151
152 if (h % 2 !== 0) {
153 h++;
154 }
155
156 const c = clone(image);
157
158 image.scan((_, __, idx) => {
159 image.bitmap.data.writeUInt32BE(image.background, idx);
160 });
161
162 const max = Math.max(w, h, image.bitmap.width, image.bitmap.height);
163 image = resizeMethods.resize(image, {
164 h: max,
165 w: max,
166 mode: mode === true ? undefined : mode,
167 });
168
169 image = composite(
170 image,
171 c,
172 image.bitmap.width / 2 - c.bitmap.width / 2,
173 image.bitmap.height / 2 - c.bitmap.height / 2
174 );
175 }

Callers 1

rotateFunction · 0.85

Calls 5

cloneFunction · 0.90
compositeFunction · 0.90
resizeMethod · 0.80
scanMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…