Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@jimp/plugin-resize/src/index.js
1126 views
1
import { throwError, isNodePattern } from '@jimp/utils';
2
3
import Resize from './modules/resize';
4
import Resize2 from './modules/resize2';
5
6
export default () => ({
7
constants: {
8
RESIZE_NEAREST_NEIGHBOR: 'nearestNeighbor',
9
RESIZE_BILINEAR: 'bilinearInterpolation',
10
RESIZE_BICUBIC: 'bicubicInterpolation',
11
RESIZE_HERMITE: 'hermiteInterpolation',
12
RESIZE_BEZIER: 'bezierInterpolation'
13
},
14
15
class: {
16
/**
17
* Resizes the image to a set width and height using a 2-pass bilinear algorithm
18
* @param {number} w the width to resize the image to (or Jimp.AUTO)
19
* @param {number} h the height to resize the image to (or Jimp.AUTO)
20
* @param {string} mode (optional) a scaling method (e.g. Jimp.RESIZE_BEZIER)
21
* @param {function(Error, Jimp)} cb (optional) a callback for when complete
22
* @returns {Jimp} this for chaining of methods
23
*/
24
resize(w, h, mode, cb) {
25
if (typeof w !== 'number' || typeof h !== 'number') {
26
return throwError.call(this, 'w and h must be numbers', cb);
27
}
28
29
if (typeof mode === 'function' && typeof cb === 'undefined') {
30
cb = mode;
31
mode = null;
32
}
33
34
if (w === this.constructor.AUTO && h === this.constructor.AUTO) {
35
return throwError.call(this, 'w and h cannot both be set to auto', cb);
36
}
37
38
if (w === this.constructor.AUTO) {
39
w = this.bitmap.width * (h / this.bitmap.height);
40
}
41
42
if (h === this.constructor.AUTO) {
43
h = this.bitmap.height * (w / this.bitmap.width);
44
}
45
46
if (w < 0 || h < 0) {
47
return throwError.call(this, 'w and h must be positive numbers', cb);
48
}
49
50
// round inputs
51
w = Math.round(w);
52
h = Math.round(h);
53
54
if (typeof Resize2[mode] === 'function') {
55
const dst = {
56
data: Buffer.alloc(w * h * 4),
57
width: w,
58
height: h
59
};
60
Resize2[mode](this.bitmap, dst);
61
this.bitmap = dst;
62
} else {
63
const image = this;
64
const resize = new Resize(
65
this.bitmap.width,
66
this.bitmap.height,
67
w,
68
h,
69
true,
70
true,
71
buffer => {
72
image.bitmap.data = Buffer.from(buffer);
73
image.bitmap.width = w;
74
image.bitmap.height = h;
75
}
76
);
77
resize.resize(this.bitmap.data);
78
}
79
80
if (isNodePattern(cb)) {
81
cb.call(this, null, this);
82
}
83
84
return this;
85
}
86
}
87
});
88
89