index.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports["default"] = pluginCrop;
  7. var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  8. var _utils = require("@jimp/utils");
  9. /* eslint-disable no-labels */
  10. function pluginCrop(event) {
  11. /**
  12. * Crops the image at a given point to a give size
  13. * @param {number} x the x coordinate to crop form
  14. * @param {number} y the y coordinate to crop form
  15. * @param w the width of the crop region
  16. * @param h the height of the crop region
  17. * @param {function(Error, Jimp)} cb (optional) a callback for when complete
  18. * @returns {Jimp} this for chaining of methods
  19. */
  20. event('crop', function (x, y, w, h, cb) {
  21. if (typeof x !== 'number' || typeof y !== 'number') return _utils.throwError.call(this, 'x and y must be numbers', cb);
  22. if (typeof w !== 'number' || typeof h !== 'number') return _utils.throwError.call(this, 'w and h must be numbers', cb); // round input
  23. x = Math.round(x);
  24. y = Math.round(y);
  25. w = Math.round(w);
  26. h = Math.round(h);
  27. if (x === 0 && w === this.bitmap.width) {
  28. // shortcut
  29. var start = w * y + x << 2;
  30. var end = start + h * w << 2;
  31. this.bitmap.data = this.bitmap.data.slice(start, end);
  32. } else {
  33. var bitmap = Buffer.allocUnsafe(w * h * 4);
  34. var offset = 0;
  35. this.scanQuiet(x, y, w, h, function (x, y, idx) {
  36. var data = this.bitmap.data.readUInt32BE(idx, true);
  37. bitmap.writeUInt32BE(data, offset, true);
  38. offset += 4;
  39. });
  40. this.bitmap.data = bitmap;
  41. }
  42. this.bitmap.width = w;
  43. this.bitmap.height = h;
  44. if ((0, _utils.isNodePattern)(cb)) {
  45. cb.call(this, null, this);
  46. }
  47. return this;
  48. });
  49. return {
  50. "class": {
  51. /**
  52. * Autocrop same color borders from this image
  53. * @param {number} tolerance (optional): a percent value of tolerance for pixels color difference (default: 0.0002%)
  54. * @param {boolean} cropOnlyFrames (optional): flag to crop only real frames: all 4 sides of the image must have some border (default: true)
  55. * @param {function(Error, Jimp)} cb (optional): a callback for when complete (default: no callback)
  56. * @returns {Jimp} this for chaining of methods
  57. */
  58. autocrop: function autocrop() {
  59. var w = this.bitmap.width;
  60. var h = this.bitmap.height;
  61. var minPixelsPerSide = 1; // to avoid cropping completely the image, resulting in an invalid 0 sized image
  62. var cb; // callback
  63. var leaveBorder = 0; // Amount of pixels in border to leave
  64. var tolerance = 0.0002; // percent of color difference tolerance (default value)
  65. var cropOnlyFrames = true; // flag to force cropping only if the image has a real "frame"
  66. // i.e. all 4 sides have some border (default value)
  67. var cropSymmetric = false; // flag to force cropping top be symmetric.
  68. // i.e. north and south / east and west are cropped by the same value
  69. // parse arguments
  70. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  71. args[_key] = arguments[_key];
  72. }
  73. for (var a = 0, len = args.length; a < len; a++) {
  74. if (typeof args[a] === 'number') {
  75. // tolerance value passed
  76. tolerance = args[a];
  77. }
  78. if (typeof args[a] === 'boolean') {
  79. // cropOnlyFrames value passed
  80. cropOnlyFrames = args[a];
  81. }
  82. if (typeof args[a] === 'function') {
  83. // callback value passed
  84. cb = args[a];
  85. }
  86. if ((0, _typeof2["default"])(args[a]) === 'object') {
  87. // config object passed
  88. var config = args[a];
  89. if (typeof config.tolerance !== 'undefined') {
  90. tolerance = config.tolerance;
  91. }
  92. if (typeof config.cropOnlyFrames !== 'undefined') {
  93. cropOnlyFrames = config.cropOnlyFrames;
  94. }
  95. if (typeof config.cropSymmetric !== 'undefined') {
  96. cropSymmetric = config.cropSymmetric;
  97. }
  98. if (typeof config.leaveBorder !== 'undefined') {
  99. leaveBorder = config.leaveBorder;
  100. }
  101. }
  102. }
  103. /**
  104. * All borders must be of the same color as the top left pixel, to be cropped.
  105. * It should be possible to crop borders each with a different color,
  106. * but since there are many ways for corners to intersect, it would
  107. * introduce unnecessary complexity to the algorithm.
  108. */
  109. // scan each side for same color borders
  110. var colorTarget = this.getPixelColor(0, 0); // top left pixel color is the target color
  111. var rgba1 = this.constructor.intToRGBA(colorTarget); // for north and east sides
  112. var northPixelsToCrop = 0;
  113. var eastPixelsToCrop = 0;
  114. var southPixelsToCrop = 0;
  115. var westPixelsToCrop = 0; // north side (scan rows from north to south)
  116. colorTarget = this.getPixelColor(0, 0);
  117. north: for (var y = 0; y < h - minPixelsPerSide; y++) {
  118. for (var x = 0; x < w; x++) {
  119. var colorXY = this.getPixelColor(x, y);
  120. var rgba2 = this.constructor.intToRGBA(colorXY);
  121. if (this.constructor.colorDiff(rgba1, rgba2) > tolerance) {
  122. // this pixel is too distant from the first one: abort this side scan
  123. break north;
  124. }
  125. } // this row contains all pixels with the same color: increment this side pixels to crop
  126. northPixelsToCrop++;
  127. } // east side (scan columns from east to west)
  128. colorTarget = this.getPixelColor(w, 0);
  129. east: for (var _x = 0; _x < w - minPixelsPerSide; _x++) {
  130. for (var _y = 0 + northPixelsToCrop; _y < h; _y++) {
  131. var _colorXY = this.getPixelColor(_x, _y);
  132. var _rgba = this.constructor.intToRGBA(_colorXY);
  133. if (this.constructor.colorDiff(rgba1, _rgba) > tolerance) {
  134. // this pixel is too distant from the first one: abort this side scan
  135. break east;
  136. }
  137. } // this column contains all pixels with the same color: increment this side pixels to crop
  138. eastPixelsToCrop++;
  139. } // south side (scan rows from south to north)
  140. colorTarget = this.getPixelColor(0, h);
  141. south: for (var _y2 = h - 1; _y2 >= northPixelsToCrop + minPixelsPerSide; _y2--) {
  142. for (var _x2 = w - eastPixelsToCrop - 1; _x2 >= 0; _x2--) {
  143. var _colorXY2 = this.getPixelColor(_x2, _y2);
  144. var _rgba2 = this.constructor.intToRGBA(_colorXY2);
  145. if (this.constructor.colorDiff(rgba1, _rgba2) > tolerance) {
  146. // this pixel is too distant from the first one: abort this side scan
  147. break south;
  148. }
  149. } // this row contains all pixels with the same color: increment this side pixels to crop
  150. southPixelsToCrop++;
  151. } // west side (scan columns from west to east)
  152. colorTarget = this.getPixelColor(w, h);
  153. west: for (var _x3 = w - 1; _x3 >= 0 + eastPixelsToCrop + minPixelsPerSide; _x3--) {
  154. for (var _y3 = h - 1; _y3 >= 0 + northPixelsToCrop; _y3--) {
  155. var _colorXY3 = this.getPixelColor(_x3, _y3);
  156. var _rgba3 = this.constructor.intToRGBA(_colorXY3);
  157. if (this.constructor.colorDiff(rgba1, _rgba3) > tolerance) {
  158. // this pixel is too distant from the first one: abort this side scan
  159. break west;
  160. }
  161. } // this column contains all pixels with the same color: increment this side pixels to crop
  162. westPixelsToCrop++;
  163. } // decide if a crop is needed
  164. var doCrop = false; // apply leaveBorder
  165. westPixelsToCrop -= leaveBorder;
  166. eastPixelsToCrop -= leaveBorder;
  167. northPixelsToCrop -= leaveBorder;
  168. southPixelsToCrop -= leaveBorder;
  169. if (cropSymmetric) {
  170. var horizontal = Math.min(eastPixelsToCrop, westPixelsToCrop);
  171. var vertical = Math.min(northPixelsToCrop, southPixelsToCrop);
  172. westPixelsToCrop = horizontal;
  173. eastPixelsToCrop = horizontal;
  174. northPixelsToCrop = vertical;
  175. southPixelsToCrop = vertical;
  176. } // make sure that crops are >= 0
  177. westPixelsToCrop = westPixelsToCrop >= 0 ? westPixelsToCrop : 0;
  178. eastPixelsToCrop = eastPixelsToCrop >= 0 ? eastPixelsToCrop : 0;
  179. northPixelsToCrop = northPixelsToCrop >= 0 ? northPixelsToCrop : 0;
  180. southPixelsToCrop = southPixelsToCrop >= 0 ? southPixelsToCrop : 0; // safety checks
  181. var widthOfRemainingPixels = w - (westPixelsToCrop + eastPixelsToCrop);
  182. var heightOfRemainingPixels = h - (southPixelsToCrop + northPixelsToCrop);
  183. if (cropOnlyFrames) {
  184. // crop image if all sides should be cropped
  185. doCrop = eastPixelsToCrop !== 0 && northPixelsToCrop !== 0 && westPixelsToCrop !== 0 && southPixelsToCrop !== 0;
  186. } else {
  187. // crop image if at least one side should be cropped
  188. doCrop = eastPixelsToCrop !== 0 || northPixelsToCrop !== 0 || westPixelsToCrop !== 0 || southPixelsToCrop !== 0;
  189. }
  190. if (doCrop) {
  191. // do the real crop
  192. this.crop(eastPixelsToCrop, northPixelsToCrop, widthOfRemainingPixels, heightOfRemainingPixels);
  193. }
  194. if ((0, _utils.isNodePattern)(cb)) {
  195. cb.call(this, null, this);
  196. }
  197. return this;
  198. }
  199. }
  200. };
  201. }
  202. //# sourceMappingURL=index.js.map