index.js 949 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { isNodePattern } from '@jimp/utils';
  2. /**
  3. * Apply a ordered dithering effect
  4. * @param {function(Error, Jimp)} cb (optional) a callback for when complete
  5. * @returns {Jimp} this for chaining of methods
  6. */
  7. function dither(cb) {
  8. const rgb565Matrix = [1, 9, 3, 11, 13, 5, 15, 7, 4, 12, 2, 10, 16, 8, 14, 6];
  9. this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, function(
  10. x,
  11. y,
  12. idx
  13. ) {
  14. const thresholdId = ((y & 3) << 2) + (x % 4);
  15. const dither = rgb565Matrix[thresholdId];
  16. this.bitmap.data[idx] = Math.min(this.bitmap.data[idx] + dither, 0xff);
  17. this.bitmap.data[idx + 1] = Math.min(
  18. this.bitmap.data[idx + 1] + dither,
  19. 0xff
  20. );
  21. this.bitmap.data[idx + 2] = Math.min(
  22. this.bitmap.data[idx + 2] + dither,
  23. 0xff
  24. );
  25. });
  26. if (isNodePattern(cb)) {
  27. cb.call(this, null, this);
  28. }
  29. return this;
  30. }
  31. export default () => ({
  32. dither565: dither,
  33. dither16: dither
  34. });