Path: blob/master/node_modules/@jimp/plugin-threshold/src/index.js
1126 views
import { isNodePattern, throwError } from '@jimp/utils';12/**3* Applies a minimum color threshold to a greyscale image. Converts image to greyscale by default4* @param {number} options object5* max: A number auto limited between 0 - 2556* replace: (optional) A number auto limited between 0 - 255 (default 255)7* autoGreyscale: (optional) A boolean whether to apply greyscale beforehand (default true)8* @param {number} cb (optional) a callback for when complete9* @return {this} this for chaining of methods10*/11export default () => ({12threshold({ max, replace = 255, autoGreyscale = true }, cb) {13if (typeof max !== 'number') {14return throwError.call(this, 'max must be a number', cb);15}1617if (typeof replace !== 'number') {18return throwError.call(this, 'replace must be a number', cb);19}2021if (typeof autoGreyscale !== 'boolean') {22return throwError.call(this, 'autoGreyscale must be a boolean', cb);23}2425max = this.constructor.limit255(max);26replace = this.constructor.limit255(replace);2728if (autoGreyscale) {29this.greyscale();30}3132this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, (x, y, idx) => {33const grey =34this.bitmap.data[idx] < max ? this.bitmap.data[idx] : replace;3536this.bitmap.data[idx] = grey;37this.bitmap.data[idx + 1] = grey;38this.bitmap.data[idx + 2] = grey;39});4041if (isNodePattern(cb)) {42cb.call(this, null, this);43}4445return this;46}47});484950