diff --git a/Gruntfile.js b/Gruntfile.js index 4b6efb69..52441e53 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -40,7 +40,8 @@ module.exports = function(grunt) { 'src/filters/Invert.js', 'src/filters/Blur.js', 'src/filters/Mask.js', - 'src/filters/Colors.js' + 'src/filters/Colors.js', + 'src/filters/Convolution.js' ]; var unitTestFiles = [ diff --git a/src/filters/Convolution.js b/src/filters/Convolution.js new file mode 100644 index 00000000..23d54e80 --- /dev/null +++ b/src/filters/Convolution.js @@ -0,0 +1,389 @@ +(function() { + + var convolve_internal = function(imageData,matrix){ + // Input data + var pixels = imageData.data, + imageSizeX = imageData.width, + imageSizeY = imageData.height, + nPixels = imageSizeX*imageSizeY, + pixel; + + // An array for storing the result + var result = []; + result.length = imageSizeX*imageSizeY*4; + + // Determine the size and demsionality of the matrix + // Note: it should be square and odd (3,5,7,9 etc...) + var is2D = (matrix[0].length > 0) || 0, + matrixSizeX = matrix.length, + matrixSizeY = matrix.length; + + // Make sure we don't try to access pixels outside the image + var xMax = Math.floor(imageSizeX - matrixSizeX/2), + xMin = Math.floor(matrixSizeX/2), + yMax = Math.floor(imageSizeY - matrixSizeY/2), + yMin = Math.floor(matrixSizeY/2); + + // Accumlators and positions for iterating + var r,g,b,a, x, y, pos, i,j; + + if( is2D ){ + for( y=yMin; y