Skip to content

Commit

Permalink
More work on convolution matrices; getting closer
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Shea committed Sep 14, 2010
1 parent e9eb4f0 commit 308e1d1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 110 deletions.
7 changes: 7 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ <h1><a href="http://github.com/mezzoblue/PaintbrushJS">PaintbrushJS</a> Playgrou
</label>
</div>

<div class="controls" id="controls-matrix">
<label>
Amount:
<input type="range" name="data-pb-matrix-amount" min="0" max="1" step="0.01" value="0.2">
</label>
</div>

<div class="controls" id="controls-mosaic">
<label>
Opacity:
Expand Down
101 changes: 0 additions & 101 deletions demo/script/paintbrush-kernel.js

This file was deleted.

42 changes: 34 additions & 8 deletions demo/script/paintbrush.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,20 +328,43 @@ function addFilter(filterType, buffer, c) {

function applyMatrix(img, pixels, params) {

// -------------
// been leaning on this a lot:
// http://forum.processing.org/topic/controlled-blur-or-edge-detect-effect-using-convolution-kernel
// -------------
// create a second buffer to hold matrix results
var buffer2 = document.createElement("canvas");
// get the canvas context
var c2 = buffer2.getContext('2d');

// set the dimensions
c2.width = buffer2.width = img.width;
c2.height = buffer2.height = img.height;

// draw the image to the new buffer
c2.drawImage(img, 0, 0, img.width , img.height);
var bufferedPixels = c2.getImageData(0, 0, c.width, c.height)

// speed up access
var data = pixels.data, imgWidth = img.width;
var data = pixels.data, bufferedData = bufferedPixels.data, imgWidth = img.width;

// 3x3 matrix can be any combination of digits, though to maintain brightness they should add up to 1
// (-1 x 8 + 9 = 1)
var matrix = [
/*
// sharpen
-1, -1, -1,
-1, 9, -1,
-1, -1, -1
*/

// emboss
-2, -1, 0,
-1, 1, 1,
0, 1, 2

/*
// reverse emboss
2, 1, 0,
1, 1, -1,
0, -1, -2
*/

/*
0, -1, 0,
Expand All @@ -350,6 +373,7 @@ function addFilter(filterType, buffer, c) {
*/

/*
// box blur
0.111, 0.111, 0.111,
0.111, 0.111, 0.111,
0.111, 0.111, 0.111
Expand Down Expand Up @@ -379,9 +403,9 @@ function addFilter(filterType, buffer, c) {

// find RGB values for that pixel
var currentPixel = {
r: data[r],
g: data[r + 1],
b: data[r + 2]
r: bufferedData[r],
g: bufferedData[r + 1],
b: bufferedData[r + 2]
};

// apply the value from the current matrix position
Expand Down Expand Up @@ -409,6 +433,8 @@ function addFilter(filterType, buffer, c) {
}
}

// code to clean the secondary buffer out of the DOM would be good here

return(pixels);
}

Expand Down
8 changes: 7 additions & 1 deletion demo/script/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ var filterControl = {
"controlId" : "controls-greyscale"
},

{
"name" : "matrix",
"label" : "Matrix",
"filterClass" : "filter-matrix",
"controlId" : "controls-matrix"
},

{
"name" : "mosaic",
"label" : "Mosaic",
Expand Down Expand Up @@ -102,7 +109,6 @@ addLoadEvent(function() {

// initialize the control panel
displayControls();
updateFilters(img);

});

Expand Down

0 comments on commit 308e1d1

Please sign in to comment.