index.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports["default"] = void 0;
  7. var _utils = require("@jimp/utils");
  8. var _resize = _interopRequireDefault(require("./modules/resize"));
  9. var _resize2 = _interopRequireDefault(require("./modules/resize2"));
  10. var _default = function _default() {
  11. return {
  12. constants: {
  13. RESIZE_NEAREST_NEIGHBOR: 'nearestNeighbor',
  14. RESIZE_BILINEAR: 'bilinearInterpolation',
  15. RESIZE_BICUBIC: 'bicubicInterpolation',
  16. RESIZE_HERMITE: 'hermiteInterpolation',
  17. RESIZE_BEZIER: 'bezierInterpolation'
  18. },
  19. "class": {
  20. /**
  21. * Resizes the image to a set width and height using a 2-pass bilinear algorithm
  22. * @param {number} w the width to resize the image to (or Jimp.AUTO)
  23. * @param {number} h the height to resize the image to (or Jimp.AUTO)
  24. * @param {string} mode (optional) a scaling method (e.g. Jimp.RESIZE_BEZIER)
  25. * @param {function(Error, Jimp)} cb (optional) a callback for when complete
  26. * @returns {Jimp} this for chaining of methods
  27. */
  28. resize: function resize(w, h, mode, cb) {
  29. if (typeof w !== 'number' || typeof h !== 'number') {
  30. return _utils.throwError.call(this, 'w and h must be numbers', cb);
  31. }
  32. if (typeof mode === 'function' && typeof cb === 'undefined') {
  33. cb = mode;
  34. mode = null;
  35. }
  36. if (w === this.constructor.AUTO && h === this.constructor.AUTO) {
  37. return _utils.throwError.call(this, 'w and h cannot both be set to auto', cb);
  38. }
  39. if (w === this.constructor.AUTO) {
  40. w = this.bitmap.width * (h / this.bitmap.height);
  41. }
  42. if (h === this.constructor.AUTO) {
  43. h = this.bitmap.height * (w / this.bitmap.width);
  44. }
  45. if (w < 0 || h < 0) {
  46. return _utils.throwError.call(this, 'w and h must be positive numbers', cb);
  47. } // round inputs
  48. w = Math.round(w);
  49. h = Math.round(h);
  50. if (typeof _resize2["default"][mode] === 'function') {
  51. var dst = {
  52. data: Buffer.alloc(w * h * 4),
  53. width: w,
  54. height: h
  55. };
  56. _resize2["default"][mode](this.bitmap, dst);
  57. this.bitmap = dst;
  58. } else {
  59. var image = this;
  60. var resize = new _resize["default"](this.bitmap.width, this.bitmap.height, w, h, true, true, function (buffer) {
  61. image.bitmap.data = Buffer.from(buffer);
  62. image.bitmap.width = w;
  63. image.bitmap.height = h;
  64. });
  65. resize.resize(this.bitmap.data);
  66. }
  67. if ((0, _utils.isNodePattern)(cb)) {
  68. cb.call(this, null, this);
  69. }
  70. return this;
  71. }
  72. }
  73. };
  74. };
  75. exports["default"] = _default;
  76. //# sourceMappingURL=index.js.map