function gaussBlur(imgData, radius, sigma){var pixes = imgData.data,
height = imgData.height,
width = imgData.width,
radius = radius ||5;
sigma = sigma || radius /3;var gaussEdge = radius *2+1;var gaussMatrix =[],
gaussSum =0,
a =1/(2* sigma * sigma *Math.PI),
b =-a *Math.PI;for(var i =-radius; i <= radius; i++){for(var j =-radius; j <= radius; j++){var gxy = a *Math.exp((i * i + j * j)* b);
gaussMatrix.push(gxy);
gaussSum += gxy;}}var gaussNum =(radius +1)*(radius +1);for(var i =0; i < gaussNum; i++){
gaussMatrix[i]/= gaussSum;}for(var x =0; x < width; x++){for(var y =0; y < height; y++){var r = g = b =0;for(var i =-radius; i<=radius; i++){var m = handleEdge(i, x, width);for(var j =-radius; j <= radius; j++){var mm = handleEdge(j, y, height);var currentPixId =(mm * width + m)*4;var jj = j + radius;var ii = i + radius;
r += pixes[currentPixId]* gaussMatrix[jj * gaussEdge + ii];
g += pixes[currentPixId +1]* gaussMatrix[jj * gaussEdge + ii];
b += pixes[currentPixId +2]* gaussMatrix[jj * gaussEdge + ii];}}var pixId =(y * width + x)*4;
pixes[pixId]= ~~r;
pixes[pixId +1]= ~~g;
pixes[pixId +2]= ~~b;}}
imgData.data= pixes;return imgData;}function handleEdge(i, x, w){var m = x + i;if(m <0){
m =-m;}elseif(m >= w){
m = w + i -x;}return m;}
function gaussBlur(imgData, radius, sigma) {
var pixes = imgData.data,
height = imgData.height,
width = imgData.width,
radius = radius || 5;
sigma = sigma || radius / 3;
var gaussEdge = radius * 2 + 1;
var gaussMatrix = [],
gaussSum = 0,
a = 1 / (2 * sigma * sigma * Math.PI),
b = -a * Math.PI;
for(var i = -radius; i <= radius; i++) {
for(var j = -radius; j <= radius; j++) {
var gxy = a * Math.exp((i * i + j * j) * b);
gaussMatrix.push(gxy);
gaussSum += gxy;
}
}
var gaussNum = (radius + 1) * (radius + 1);
for(var i = 0; i < gaussNum; i++) {
gaussMatrix[i] /= gaussSum;
}
for(var x = 0; x < width; x++) {
for(var y = 0; y < height; y++) {
var r = g = b = 0;
for(var i = -radius; i<=radius; i++) {
var m = handleEdge(i, x, width);
for(var j = -radius; j <= radius; j++) {
var mm = handleEdge(j, y, height);
var currentPixId = (mm * width + m) * 4;
var jj = j + radius;
var ii = i + radius;
r += pixes[currentPixId] * gaussMatrix[jj * gaussEdge + ii];
g += pixes[currentPixId + 1] * gaussMatrix[jj * gaussEdge + ii];
b += pixes[currentPixId + 2] * gaussMatrix[jj * gaussEdge + ii];
}
}
var pixId = (y * width + x) * 4;
pixes[pixId] = ~~r;
pixes[pixId + 1] = ~~g;
pixes[pixId + 2] = ~~b;
}
}
imgData.data = pixes;
return imgData;
}
function handleEdge(i, x, w) {
var m = x + i;
if(m < 0) {
m = -m;
} else if(m >= w) {
m = w + i -x;
}
return m;
}
怀旧滤镜
怀旧滤镜公式
for(var i =0; i < imgData.height* imgData.width; i++){var r = imgData.data[i*4],
g = imgData.data[i*4+1],
b = imgData.data[i*4+2];var newR =(0.393* r +0.769* g +0.189* b);var newG =(0.349* r +0.686* g +0.168* b);var newB =(0.272* r +0.534* g +0.131* b);var rgbArr =[newR, newG, newB].map((e)=>{return e <0?0: e >255?255: e;});[imgData.data[i*4], imgData.data[i*4+1], imgData.data[i*4+2]]= rgbArr;}
for(var i = 0; i < imgData.height * imgData.width; i++) {
var r = imgData.data[i*4],
g = imgData.data[i*4+1],
b = imgData.data[i*4+2];
var newR = (0.393 * r + 0.769 * g + 0.189 * b);
var newG = (0.349 * r + 0.686 * g + 0.168 * b);
var newB = (0.272 * r + 0.534 * g + 0.131 * b);
var rgbArr = [newR, newG, newB].map((e) => {
return e < 0 ? 0 : e > 255 ? 255 : e;
});
[imgData.data[i*4], imgData.data[i*4+1], imgData.data[i*4+2]] = rgbArr;
}
熔铸滤镜
公式: R = r * 128 / ( g + b + 1 ); G = g * 128 / ( r + b + 1 ); B = b * 128 / ( g + r + 1 );
for(var i =0; i < imgData.height* imgData.width; i++){var r = imgData.data[i*4],
g = imgData.data[i*4+1],
b = imgData.data[i*4+2];var newR = r *128/(g + b +1);var newG = g *128/(r + b +1);var newB = b *128/(g + r +1);var rgbArr =[newR, newG, newB].map((e)=>{return e <0?0: e >255?255: e;});[imgData.data[i*4], imgData.data[i*4+1], imgData.data[i*4+2]]= rgbArr;}
for(var i = 0; i < imgData.height * imgData.width; i++) {
var r = imgData.data[i*4],
g = imgData.data[i*4+1],
b = imgData.data[i*4+2];
var newR = r * 128 / (g + b + 1);
var newG = g * 128 / (r + b + 1);
var newB = b * 128 / (g + r + 1);
var rgbArr = [newR, newG, newB].map((e) => {
return e < 0 ? 0 : e > 255 ? 255 : e;
});
[imgData.data[i*4], imgData.data[i*4+1], imgData.data[i*4+2]] = rgbArr;
}
冰冻滤镜
公式: R = ( r – g – b ) * 3 / 2; G = ( g – r – b ) * 3 / 2; B = ( b – g – r ) * 3 / 2;
for(var i =0; i < imgData.height* imgData.width; i++){var r = imgData.data[i*4],
g = imgData.data[i*4+1],
b = imgData.data[i*4+2];var newR =(r - g -b)*3/2;var newG =(g - r -b)*3/2;var newB =(b - g -r)*3/2;var rgbArr =[newR, newG, newB].map((e)=>{return e <0?0: e >255?255: e;});[imgData.data[i*4], imgData.data[i*4+1], imgData.data[i*4+2]]= rgbArr;}
for(var i = 0; i < imgData.height * imgData.width; i++) {
var r = imgData.data[i*4],
g = imgData.data[i*4+1],
b = imgData.data[i*4+2];
var newR = (r - g -b) * 3 /2;
var newG = (g - r -b) * 3 /2;
var newB = (b - g -r) * 3 /2;
var rgbArr = [newR, newG, newB].map((e) => {
return e < 0 ? 0 : e > 255 ? 255 : e;
});
[imgData.data[i*4], imgData.data[i*4+1], imgData.data[i*4+2]] = rgbArr;
}
连环画滤镜
公式: R = |g – b + g + r| * r / 256; G = |b – g + b + r| * r / 256; B = |b – g + b + r| * g / 256;
for(var i =0; i < imgData.height* imgData.width; i++){var r = imgData.data[i*4],
g = imgData.data[i*4+1],
b = imgData.data[i*4+2];var newR =Math.abs(g - b + g + r)* r /256;var newG =Math.abs(b -g + b + r)* r /256;var newB =Math.abs(b -g + b + r)* g /256;var rgbArr =[newR, newG, newB];[imgData.data[i*4], imgData.data[i*4+1], imgData.data[i*4+2]]= rgbArr;}
for(var i = 0; i < imgData.height * imgData.width; i++) {
var r = imgData.data[i*4],
g = imgData.data[i*4+1],
b = imgData.data[i*4+2];
var newR = Math.abs(g - b + g + r) * r / 256;
var newG = Math.abs(b -g + b + r) * r / 256;
var newB = Math.abs(b -g + b + r) * g / 256;
var rgbArr = [newR, newG, newB];
[imgData.data[i*4], imgData.data[i*4+1], imgData.data[i*4+2]] = rgbArr;
}
褐色滤镜
公式: R = r * 0.393 + g * 0.769 + b * 0.189; G = r * 0.349 + g * 0.686 + b * 0.168; B = r * 0.272 + g * 0.534 + b * 0.131;
for(var i =0; i < imgData.height* imgData.width; i++){var r = imgData.data[i *4],
g = imgData.data[i *4+1],
b = imgData.data[i *4+2];var newR = r *0.393+ g *0.769+ b *0.189;var newG = r *0.349+ g *0.686+ b *0.168;var newB = r *0.272+ g *0.534+ b *0.131;var rgbArr =[newR, newG, newB];[imgData.data[i *4], imgData.data[i *4+1], imgData.data[i *4+2]]= rgbArr;}
for (var i = 0; i < imgData.height * imgData.width; i++) {
var r = imgData.data[i * 4],
g = imgData.data[i * 4 + 1],
b = imgData.data[i * 4 + 2];
var newR = r * 0.393 + g * 0.769 + b * 0.189;
var newG = r * 0.349 + g * 0.686 + b * 0.168;
var newB = r * 0.272 + g * 0.534 + b * 0.131;
var rgbArr = [newR, newG, newB];
[imgData.data[i * 4], imgData.data[i * 4 + 1], imgData.data[i * 4 + 2]] = rgbArr;
}