index.js 2.0 KB

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