index.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { isNodePattern, throwError } from '@jimp/utils';
  2. /**
  3. * Flip the image horizontally
  4. * @param {boolean} horizontal a Boolean, if true the image will be flipped horizontally
  5. * @param {boolean} vertical a Boolean, if true the image will be flipped vertically
  6. * @param {function(Error, Jimp)} cb (optional) a callback for when complete
  7. * @returns {Jimp} this for chaining of methods
  8. */
  9. function flipFn(horizontal, vertical, cb) {
  10. if (typeof horizontal !== 'boolean' || typeof vertical !== 'boolean')
  11. return throwError.call(
  12. this,
  13. 'horizontal and vertical must be Booleans',
  14. cb
  15. );
  16. const bitmap = Buffer.alloc(this.bitmap.data.length);
  17. this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, function(
  18. x,
  19. y,
  20. idx
  21. ) {
  22. const _x = horizontal ? this.bitmap.width - 1 - x : x;
  23. const _y = vertical ? this.bitmap.height - 1 - y : y;
  24. const _idx = (this.bitmap.width * _y + _x) << 2;
  25. const data = this.bitmap.data.readUInt32BE(idx);
  26. bitmap.writeUInt32BE(data, _idx);
  27. });
  28. this.bitmap.data = Buffer.from(bitmap);
  29. if (isNodePattern(cb)) {
  30. cb.call(this, null, this);
  31. }
  32. return this;
  33. }
  34. export default () => ({
  35. flip: flipFn,
  36. mirror: flipFn
  37. });