function sobel(image, width, height) {
var temp = new Array(image.length / 4);
function get_index(_x, _y) {
return (width * _y + _x) * 4;
}
// モノクロにする
for (var i = 0; i < image.length; i += 4) {
image[i] = Math.floor((image[i] + image[i+1] + image[i+2]) / 3);
}
for (var y = 1; y < height - 1; y++) {
var index = width * y * 4;
for (var x = 1; x < width - 1; x++) {
var ghs = image[ index + (x - 1 - width) * 4 ] * -1
+ image[ index + (x + 1 - width) * 4 ]
+ image[ index + (x - 1) * 4 ] * -2
+ image[ index + (x + 1) * 4 ] * 2
+ image[ index + (x - 1 + width) * 4 ] * -1
+ image[ index + (x + 1 + width) * 4 ];
var gvs = image[ index + (x - 1 - width) * 4 ] * -1
+ image[ index + (x - width) * 4 ] * -2
+ image[ index + (x + 1 - width) * 4 ] * -1
+ image[ index + (x - 1 + width) * 4 ]
+ image[ index + (x + width) * 4 ] * 2
+ image[ index + (x + 1 + width) * 4 ];
var g = Math.floor(Math.sqrt(ghs * ghs + gvs * gvs));
index = get_index(x, y) / 4;
temp[ index ] = 255 - g;
}
}
for (var i = 0; i < image.length; i += 4) {
image[i] = image[i+1] = image[i+2] = temp[i / 4];
}
}
function gaussian3x3(image, width, height) {
var temp = new Array(image.length);
function get_index(_x, _y) {
return (width * _y + _x) * 4;
}
function filter(_x, _y, _i) {
return (
image[ get_index(_x-1, _y-1) + _i ]
+ image[ get_index(_x , _y-1) + _i ] * 2
+ image[ get_index(_x+1, _y-1) + _i ]
+ image[ get_index(_x-1, _y ) + _i ] * 2
+ image[ get_index(_x , _y ) + _i ] * 4
+ image[ get_index(_x-1, _y ) + _i ] * 2
+ image[ get_index(_x-1, _y+1) + _i ]
+ image[ get_index(_x , _y+1) + _i ] * 2
+ image[ get_index(_x+1, _y+1) + _i ]
) / 16;
}
for (var i = 0; i < image.length; i += 4) {
temp[i ] = image[i ];
temp[i + 1] = image[i + 1];
temp[i + 2] = image[i + 2];
}
for (var y = 1; y < height - 1; y++) {
for (var x = 1; x < width - 1; x++) {
var index = get_index(x, y);
temp[index ] = filter(x, y, 0);
temp[index + 1] = filter(x, y, 1);
temp[index + 2] = filter(x, y, 2);
}
}
for (var i = 0; i < image.length; i += 4) {
image[i ] = temp[i ];
image[i+1] = temp[i+1];
image[i+2] = temp[i+2];
}
}