index.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. require("core-js/modules/es.regexp.exec");
  3. require("core-js/modules/es.string.replace");
  4. Object.defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports["default"] = void 0;
  8. var _utils = require("@jimp/utils");
  9. /**
  10. * Applies a minimum color threshold to a greyscale image. Converts image to greyscale by default
  11. * @param {number} options object
  12. * max: A number auto limited between 0 - 255
  13. * replace: (optional) A number auto limited between 0 - 255 (default 255)
  14. * autoGreyscale: (optional) A boolean whether to apply greyscale beforehand (default true)
  15. * @param {number} cb (optional) a callback for when complete
  16. * @return {this} this for chaining of methods
  17. */
  18. var _default = function _default() {
  19. return {
  20. threshold: function threshold(_ref, cb) {
  21. var _this = this;
  22. var max = _ref.max,
  23. _ref$replace = _ref.replace,
  24. replace = _ref$replace === void 0 ? 255 : _ref$replace,
  25. _ref$autoGreyscale = _ref.autoGreyscale,
  26. autoGreyscale = _ref$autoGreyscale === void 0 ? true : _ref$autoGreyscale;
  27. if (typeof max !== 'number') {
  28. return _utils.throwError.call(this, 'max must be a number', cb);
  29. }
  30. if (typeof replace !== 'number') {
  31. return _utils.throwError.call(this, 'replace must be a number', cb);
  32. }
  33. if (typeof autoGreyscale !== 'boolean') {
  34. return _utils.throwError.call(this, 'autoGreyscale must be a boolean', cb);
  35. }
  36. max = this.constructor.limit255(max);
  37. replace = this.constructor.limit255(replace);
  38. if (autoGreyscale) {
  39. this.greyscale();
  40. }
  41. this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, function (x, y, idx) {
  42. var grey = _this.bitmap.data[idx] < max ? _this.bitmap.data[idx] : replace;
  43. _this.bitmap.data[idx] = grey;
  44. _this.bitmap.data[idx + 1] = grey;
  45. _this.bitmap.data[idx + 2] = grey;
  46. });
  47. if ((0, _utils.isNodePattern)(cb)) {
  48. cb.call(this, null, this);
  49. }
  50. return this;
  51. }
  52. };
  53. };
  54. exports["default"] = _default;
  55. module.exports = exports.default;
  56. //# sourceMappingURL=index.js.map