index.js 9.6 KB

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