Added sharpen filter

This commit is contained in:
Devine Lu Linvega
2017-02-22 16:05:11 -07:00
parent d163987e9a
commit 1d5e4ec476
10 changed files with 110 additions and 27 deletions

View File

@@ -64,5 +64,4 @@ function Filter_Chromatic()
ronin.surface.active_layer.clear();
context.putImageData(imageData, 0, 0);
}
}

View File

@@ -13,6 +13,29 @@ function Filter()
console.log("render: Nothing here.");
}
this.get_neighbors = function(pixels,x,y)
{
return [
this.get_color(pixels,x-1,y-1),this.get_color(pixels,x,y-1),this.get_color(pixels,x+1,y-1),
this.get_color(pixels,x-1,y),this.get_color(pixels,x,y),this.get_color(pixels,x+1,y),
this.get_color(pixels,x-1,y+1),this.get_color(pixels,x,y+1),this.get_color(pixels,x+1,y+1),
];
}
this.get_neighbors_average = function(pixels,x,y,weight_map = [1,1,1,1,1,1,1,1,1])
{
var n = this.get_neighbors(pixels,x,y);
var r = 0;
var g = 0;
var b = 0;
for (var i = 0; i < n.length; i++){
r += n[i].r * weight_map[i];
g += n[i].g * weight_map[i];
b += n[i].b * weight_map[i];
}
return {r:r/n.length,g:g/n.length,b:b/n.length}
}
this.set_color = function(pixels, color, x, y)
{
x = Math.max(0,Math.min(x,pixels.width-1));

View File

@@ -1,4 +1,4 @@
function Filter_Saturation()
function Filter_Grey()
{
Filter.call(this);

View File

@@ -0,0 +1,51 @@
function Filter_Sharpen()
{
Filter.call(this);
this.parameters = [];
this.render = function(cmd)
{
var imageObj = new Image();
imageObj.src = ronin.surface.active_layer.element.toDataURL('image/png');
var w = ronin.surface.size.width;
var h = ronin.surface.size.height;
var context = ronin.surface.active_layer.context();
var originalData = context.getImageData(0, 0, w*2, h*2);
var data = originalData.data;
var newImage = context.getImageData(0, 0, w*2, h*2);
var weight_map = [
-2, 0, -2,
0, 16, 0,
-2, 0, -2
];
for(var i = 0; i < data.length; i += 4) {
var p = i / 4;
var x = (p % originalData.width);
var y = parseInt(p/originalData.width);
var average = this.get_neighbors_average(originalData,x,y,weight_map);
newImage.data[i] = parseInt(average.r);
newImage.data[i+1] = parseInt(average.g);
newImage.data[i+2] = parseInt(average.b);
}
ronin.surface.active_layer.clear();
context.putImageData(newImage, 0, 0);
}
this.preview = function(cmd)
{
}
this.draw = function(context = this.context(), value, position)
{
}
}