lib/operation.js
function convolve (kernel) {
  if (!is.object(kernel) || !Array.isArray(kernel.kernel) ||
      !is.integer(kernel.width) || !is.integer(kernel.height) ||
      !is.inRange(kernel.width, 3, 1001) || !is.inRange(kernel.height, 3, 1001) ||
      kernel.height * kernel.width !== kernel.kernel.length
  ) {
    // must pass in a kernel
    throw new Error('Invalid convolution kernel');
  }
  // Default scale is sum of kernel values
  if (!is.integer(kernel.scale)) {
    kernel.scale = kernel.kernel.reduce(function (a, b) {
      return a + b;
    }, 0);
  }
  // Clip scale to a minimum value of 1
  if (kernel.scale < 1) {
    kernel.scale = 1;
  }
  if (!is.integer(kernel.offset)) {
    kernel.offset = 0;
  }
  this.options.convKernel = kernel;
  return this;
}
function convolve (kernel) {
  if (!is.object(kernel) || !Array.isArray(kernel.kernel) ||
      !is.integer(kernel.width) || !is.integer(kernel.height) ||
      !is.inRange(kernel.width, 3, 1001) || !is.inRange(kernel.height, 3, 1001) ||
      kernel.height * kernel.width !== kernel.kernel.length
  ) {
    // must pass in a kernel
    throw new Error('Invalid convolution kernel');
  }
  // Default scale is sum of kernel values
  if (!is.integer(kernel.scale)) {
    kernel.scale = kernel.kernel.reduce(function (a, b) {
      return a + b;
    }, 0);
  }
  // Clip scale to a minimum value of 1
  if (kernel.scale < 1) {
    kernel.scale = 1;
  }
  if (!is.integer(kernel.offset)) {
    kernel.offset = 0;
  }
  this.options.convKernel = kernel;
  return this;
}
function convolve ( kernel ) { if ( ! is . object ( kernel ) || ! Array . isArray ( kernel . kernel ) || ! is . integer ( kernel . width ) || ! is . integer ( kernel . height ) || ! is . inRange ( kernel . width , 3 , 1001 ) || ! is . inRange ( kernel . height , 3 , 1001 ) || kernel . height * kernel . width !== kernel . kernel . length ) { // must pass in a kernel throw new Error ( 'Invalid convolution kernel' ) ; } // Default scale is sum of kernel values if ( ! is . integer ( kernel . scale ) ) { kernel . scale = kernel . kernel . reduce ( function ( a , b ) { return a + b ; } , 0 ) ; } // Clip scale to a minimum value of 1 if ( kernel . scale < 1 ) { kernel . scale = 1 ; } if ( ! is . integer ( kernel . offset ) ) { kernel . offset = 0 ; } this . options . convKernel = kernel ; return this ; }
Convolve the image with the specified kernel.

@example
sharp(input)
.convolve({
width: 3,
height: 3,
kernel: [-1, 0, 1, -2, 0, 2, -1, 0, 1]
})
.raw()
.toBuffer(function(err, data, info) {
// data contains the raw pixel data representing the convolution
// of the input image with the horizontal Sobel operator
});

@param {Object} kernel
@param {Number} kernel.width - width of the kernel in pixels.
@param {Number} kernel.height - width of the kernel in pixels.
@param {Array<Number>} kernel.kernel - Array of length `width*height` containing the kernel values.
@param {Number} [kernel.scale=sum] - the scale of the kernel in pixels.
@param {Number} [kernel.offset=0] - the offset of the kernel in pixels.
@returns {Sharp}
@throws {Error} Invalid parameters
Convolve the image with the specified kernel .