Path: blob/master/node_modules/@jimp/plugin-dither/src/index.js
1126 views
import { isNodePattern } from '@jimp/utils';12/**3* Apply a ordered dithering effect4* @param {function(Error, Jimp)} cb (optional) a callback for when complete5* @returns {Jimp} this for chaining of methods6*/7function dither(cb) {8const rgb565Matrix = [1, 9, 3, 11, 13, 5, 15, 7, 4, 12, 2, 10, 16, 8, 14, 6];9this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, function(10x,11y,12idx13) {14const thresholdId = ((y & 3) << 2) + (x % 4);15const dither = rgb565Matrix[thresholdId];16this.bitmap.data[idx] = Math.min(this.bitmap.data[idx] + dither, 0xff);17this.bitmap.data[idx + 1] = Math.min(18this.bitmap.data[idx + 1] + dither,190xff20);21this.bitmap.data[idx + 2] = Math.min(22this.bitmap.data[idx + 2] + dither,230xff24);25});2627if (isNodePattern(cb)) {28cb.call(this, null, this);29}3031return this;32}3334export default () => ({35dither565: dither,36dither16: dither37});383940