Skip to content

Commit

Permalink
Expose convolution function (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsonfellipe authored Oct 9, 2020
1 parent e7aba97 commit 2ce9448
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
20 changes: 10 additions & 10 deletions lib/operations/convolution.js
Original file line number Diff line number Diff line change
@@ -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++) {
Expand All @@ -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
Expand Down

0 comments on commit 2ce9448

Please sign in to comment.