Skip to content

Commit

Permalink
Merge pull request fabricjs#2634 from asturur/convolutefilte
Browse files Browse the repository at this point in the history
General code clean up for convolute filter
  • Loading branch information
kangax committed Nov 18, 2015
2 parents 540ecc6 + fe7462c commit a25459c
Showing 1 changed file with 12 additions and 29 deletions.
41 changes: 12 additions & 29 deletions src/filters/convolute_filter.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,6 @@
0, 1, 0,
0, 0, 0
];

var canvasEl = fabric.util.createCanvasElement();
this.tmpCtx = canvasEl.getContext('2d');
},

/**
* @private
*/
_createImageData: function(w, h) {
return this.tmpCtx.createImageData(w, h);
},

/**
Expand All @@ -98,39 +88,32 @@
src = pixels.data,
sw = pixels.width,
sh = pixels.height,

// pad output by the convolution matrix
w = sw,
h = sh,
output = this._createImageData(w, h),

output = context.createImageData(sw, sh),
dst = output.data,

// go through the destination image pixels
alphaFac = this.opaque ? 1 : 0;
alphaFac = this.opaque ? 1 : 0,
r, g, b, a, dstOff,
scx, scy, srcOff, wt;

for (var y = 0; y < h; y++) {
for (var x = 0; x < w; x++) {
var sy = y,
sx = x,
dstOff = (y * w + x) * 4,
for (var y = 0; y < sh; y++) {
for (var x = 0; x < sw; x++) {
dstOff = (y * sh + x) * 4;
// calculate the weighed sum of the source image pixels that
// fall under the convolution matrix
r = 0, g = 0, b = 0, a = 0;
r = 0; g = 0; b = 0; a = 0;

for (var cy = 0; cy < side; cy++) {
for (var cx = 0; cx < side; cx++) {

var scy = sy + cy - halfSide,
scx = sx + cx - halfSide;
scy = y + cy - halfSide;
scx = x + cx - halfSide;

/* jshint maxdepth:5 */
if (scy < 0 || scy > sh || scx < 0 || scx > sw) {
continue;
}

var srcOff = (scy * sw + scx) * 4,
wt = weights[cy * side + cx];
srcOff = (scy * sw + scx) * 4;
wt = weights[cy * side + cx];

r += src[srcOff] * wt;
g += src[srcOff + 1] * wt;
Expand Down

0 comments on commit a25459c

Please sign in to comment.