|
|
import { isNodePattern, throwError } from "@jimp/utils"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default () => ({ |
|
|
threshold({ max, replace = 255, autoGreyscale = true }, cb) { |
|
|
if (typeof max !== "number") { |
|
|
return throwError.call(this, "max must be a number", cb); |
|
|
} |
|
|
|
|
|
if (typeof replace !== "number") { |
|
|
return throwError.call(this, "replace must be a number", cb); |
|
|
} |
|
|
|
|
|
if (typeof autoGreyscale !== "boolean") { |
|
|
return throwError.call(this, "autoGreyscale must be a boolean", cb); |
|
|
} |
|
|
|
|
|
max = this.constructor.limit255(max); |
|
|
replace = this.constructor.limit255(replace); |
|
|
|
|
|
if (autoGreyscale) { |
|
|
this.greyscale(); |
|
|
} |
|
|
|
|
|
this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, (x, y, idx) => { |
|
|
const grey = |
|
|
this.bitmap.data[idx] < max ? this.bitmap.data[idx] : replace; |
|
|
|
|
|
this.bitmap.data[idx] = grey; |
|
|
this.bitmap.data[idx + 1] = grey; |
|
|
this.bitmap.data[idx + 2] = grey; |
|
|
}); |
|
|
|
|
|
if (isNodePattern(cb)) { |
|
|
cb.call(this, null, this); |
|
|
} |
|
|
|
|
|
return this; |
|
|
}, |
|
|
}); |
|
|
|