From 2ce9448af4dc7637401bfc98ac15954f06889ce5 Mon Sep 17 00:00:00 2001 From: Davidson Fellipe Date: Fri, 9 Oct 2020 15:45:02 -0400 Subject: [PATCH] Expose convolution function (#46) --- lib/index.js | 2 +- lib/operations/convolution.js | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/index.js b/lib/index.js index 3d5428a..0ed38b6 100644 --- a/lib/index.js +++ b/lib/index.js @@ -26,12 +26,12 @@ export { default as prewittHorizontal } from './filters/prewittHorizontal' export { default as sharpen } from './filters/sharpen' export { default as sobelVertical } from './filters/sobelVertical' export { default as sobelHorizontal } from './filters/sobelHorizontal' - export { default as gaussian } from './filters/gaussian' export { default as bigGaussian } from './filters/bigGaussian' export { default as canny } from './filters/canny' // utils +export { default as convolution } from './operations/convolution' export { default as printCanvas } from './utils/printCanvas' export { default as getImage } from './utils/getImage' export { default as filterImage } from './utils/filterImage' diff --git a/lib/operations/convolution.js b/lib/operations/convolution.js index dd33251..afd39c3 100644 --- a/lib/operations/convolution.js +++ b/lib/operations/convolution.js @@ -1,12 +1,12 @@ -const convolution = function (pixels, weights) { - let side = Math.round(Math.sqrt(weights.length)), - halfSide = Math.floor(side / 2), - src = pixels.data, - canvasWidth = pixels.width, - canvasHeight = pixels.height, - temporaryCanvas = document.createElement('canvas'), - temporaryCtx = temporaryCanvas.getContext('2d'), - outputData = temporaryCtx.createImageData(canvasWidth, canvasHeight) +const convolution = function (pixels, kernel) { + let side = Math.round(Math.sqrt(kernel.length)) + let halfSide = Math.floor(side / 2) + let src = pixels.data + let canvasWidth = pixels.width + let canvasHeight = pixels.height + let temporaryCanvas = document.createElement('canvas') + let temporaryCtx = temporaryCanvas.getContext('2d') + let outputData = temporaryCtx.createImageData(canvasWidth, canvasHeight) for (let y = 0; y < canvasHeight; y++) { for (let x = 0; x < canvasWidth; x++) { @@ -27,7 +27,7 @@ const convolution = function (pixels, weights) { currentKernelX < canvasWidth ) { let offset = (currentKernelY * canvasWidth + currentKernelX) * 4, - weight = weights[kernelY * side + kernelX] + weight = kernel[kernelY * side + kernelX] sumReds += src[offset] * weight sumGreens += src[offset + 1] * weight