Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@jimp/plugin-threshold/src/index.js
1126 views
1
import { isNodePattern, throwError } from '@jimp/utils';
2
3
/**
4
* Applies a minimum color threshold to a greyscale image. Converts image to greyscale by default
5
* @param {number} options object
6
* max: A number auto limited between 0 - 255
7
* replace: (optional) A number auto limited between 0 - 255 (default 255)
8
* autoGreyscale: (optional) A boolean whether to apply greyscale beforehand (default true)
9
* @param {number} cb (optional) a callback for when complete
10
* @return {this} this for chaining of methods
11
*/
12
export default () => ({
13
threshold({ max, replace = 255, autoGreyscale = true }, cb) {
14
if (typeof max !== 'number') {
15
return throwError.call(this, 'max must be a number', cb);
16
}
17
18
if (typeof replace !== 'number') {
19
return throwError.call(this, 'replace must be a number', cb);
20
}
21
22
if (typeof autoGreyscale !== 'boolean') {
23
return throwError.call(this, 'autoGreyscale must be a boolean', cb);
24
}
25
26
max = this.constructor.limit255(max);
27
replace = this.constructor.limit255(replace);
28
29
if (autoGreyscale) {
30
this.greyscale();
31
}
32
33
this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, (x, y, idx) => {
34
const grey =
35
this.bitmap.data[idx] < max ? this.bitmap.data[idx] : replace;
36
37
this.bitmap.data[idx] = grey;
38
this.bitmap.data[idx + 1] = grey;
39
this.bitmap.data[idx + 2] = grey;
40
});
41
42
if (isNodePattern(cb)) {
43
cb.call(this, null, this);
44
}
45
46
return this;
47
}
48
});
49
50