index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. * Rotates an image clockwise by an arbitrary number of degrees. NB: 'this' must be a Jimp object.
  9. * @param {number} deg the number of degrees to rotate the image by
  10. * @param {string|boolean} mode (optional) resize mode or a boolean, if false then the width and height of the image will not be changed
  11. */
  12. function advancedRotate(deg, mode) {
  13. deg %= 360;
  14. var rad = deg * Math.PI / 180;
  15. var cosine = Math.cos(rad);
  16. var sine = Math.sin(rad); // the final width and height will change if resize == true
  17. var w = this.bitmap.width;
  18. var h = this.bitmap.height;
  19. if (mode === true || typeof mode === 'string') {
  20. // resize the image to it maximum dimension and blit the existing image
  21. // onto the center so that when it is rotated the image is kept in bounds
  22. // http://stackoverflow.com/questions/3231176/how-to-get-size-of-a-rotated-rectangle
  23. // Plus 1 border pixel to ensure to show all rotated result for some cases.
  24. w = Math.ceil(Math.abs(this.bitmap.width * cosine) + Math.abs(this.bitmap.height * sine)) + 1;
  25. h = Math.ceil(Math.abs(this.bitmap.width * sine) + Math.abs(this.bitmap.height * cosine)) + 1; // Ensure destination to have even size to a better result.
  26. if (w % 2 !== 0) {
  27. w++;
  28. }
  29. if (h % 2 !== 0) {
  30. h++;
  31. }
  32. var c = this.cloneQuiet();
  33. this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, function (x, y, idx) {
  34. this.bitmap.data.writeUInt32BE(this._background, idx);
  35. });
  36. var max = Math.max(w, h, this.bitmap.width, this.bitmap.height);
  37. this.resize(max, max, mode);
  38. this.blit(c, this.bitmap.width / 2 - c.bitmap.width / 2, this.bitmap.height / 2 - c.bitmap.height / 2);
  39. }
  40. var bW = this.bitmap.width;
  41. var bH = this.bitmap.height;
  42. var dstBuffer = Buffer.alloc(this.bitmap.data.length);
  43. function createTranslationFunction(deltaX, deltaY) {
  44. return function (x, y) {
  45. return {
  46. x: x + deltaX,
  47. y: y + deltaY
  48. };
  49. };
  50. }
  51. var translate2Cartesian = createTranslationFunction(-(bW / 2), -(bH / 2));
  52. var translate2Screen = createTranslationFunction(bW / 2 + 0.5, bH / 2 + 0.5);
  53. for (var y = 1; y <= bH; y++) {
  54. for (var x = 1; x <= bW; x++) {
  55. var cartesian = translate2Cartesian(x, y);
  56. var source = translate2Screen(cosine * cartesian.x - sine * cartesian.y, cosine * cartesian.y + sine * cartesian.x);
  57. var dstIdx = bW * (y - 1) + x - 1 << 2;
  58. if (source.x >= 0 && source.x < bW && source.y >= 0 && source.y < bH) {
  59. var srcIdx = (bW * (source.y | 0) + source.x | 0) << 2;
  60. var pixelRGBA = this.bitmap.data.readUInt32BE(srcIdx);
  61. dstBuffer.writeUInt32BE(pixelRGBA, dstIdx);
  62. } else {
  63. // reset off-image pixels
  64. dstBuffer.writeUInt32BE(this._background, dstIdx);
  65. }
  66. }
  67. }
  68. this.bitmap.data = dstBuffer;
  69. if (mode === true || typeof mode === 'string') {
  70. // now crop the image to the final size
  71. var _x = bW / 2 - w / 2;
  72. var _y = bH / 2 - h / 2;
  73. this.crop(_x, _y, w, h);
  74. }
  75. }
  76. var _default = function _default() {
  77. return {
  78. /**
  79. * Rotates the image clockwise by a number of degrees. By default the width and height of the image will be resized appropriately.
  80. * @param {number} deg the number of degrees to rotate the image by
  81. * @param {string|boolean} mode (optional) resize mode or a boolean, if false then the width and height of the image will not be changed
  82. * @param {function(Error, Jimp)} cb (optional) a callback for when complete
  83. * @returns {Jimp} this for chaining of methods
  84. */
  85. rotate: function rotate(deg, mode, cb) {
  86. // enable overloading
  87. if (typeof mode === 'undefined' || mode === null) {
  88. // e.g. image.resize(120);
  89. // e.g. image.resize(120, null, cb);
  90. // e.g. image.resize(120, undefined, cb);
  91. mode = true;
  92. }
  93. if (typeof mode === 'function' && typeof cb === 'undefined') {
  94. // e.g. image.resize(120, cb);
  95. cb = mode;
  96. mode = true;
  97. }
  98. if (typeof deg !== 'number') {
  99. return _utils.throwError.call(this, 'deg must be a number', cb);
  100. }
  101. if (typeof mode !== 'boolean' && typeof mode !== 'string') {
  102. return _utils.throwError.call(this, 'mode must be a boolean or a string', cb);
  103. }
  104. advancedRotate.call(this, deg, mode, cb);
  105. if ((0, _utils.isNodePattern)(cb)) {
  106. cb.call(this, null, this);
  107. }
  108. return this;
  109. }
  110. };
  111. };
  112. exports["default"] = _default;
  113. //# sourceMappingURL=index.js.map