You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
2.5 KiB
120 lines
2.5 KiB
package com.jotuntech.sketcher.common.filter;
|
|
|
|
import com.jotuntech.sketcher.common.Pixels;
|
|
|
|
public class BlurFilter implements Filter {
|
|
float xAlpha, xRed, xGreen, xBlue, cutoff;
|
|
float[] yAlpha, yRed, yGreen, yBlue;
|
|
int width, height, x, y;
|
|
boolean xAlphaWait = false;
|
|
boolean[] yAlphaWait;
|
|
|
|
public BlurFilter() {
|
|
|
|
}
|
|
|
|
public void setSize(int width, int height) {
|
|
this.width = width;
|
|
this.height = height;
|
|
}
|
|
|
|
public void setParameterA(float a) {
|
|
this.cutoff = 1 / a;
|
|
}
|
|
|
|
public void setParameterB(float b) { }
|
|
|
|
public void setParameterC(float c) { }
|
|
|
|
public int processPass1Pixel(int pixel) {
|
|
float inAlpha = Pixels.getChannel0(pixel);
|
|
float inRed = Pixels.getChannel1(pixel);
|
|
float inGreen = Pixels.getChannel2(pixel);
|
|
float inBlue = Pixels.getChannel3(pixel);
|
|
|
|
float outAlpha, outRed, outGreen, outBlue;
|
|
if(x == 0) {
|
|
xAlpha = inAlpha;
|
|
xRed = inRed;
|
|
xGreen = inGreen;
|
|
xBlue = inBlue;
|
|
} else {
|
|
xAlpha += (inAlpha - xAlpha) * cutoff;
|
|
if(inAlpha < 1) {
|
|
xAlphaWait = true;
|
|
} else {
|
|
xAlphaWait = false;
|
|
xRed += (inRed - xRed) * cutoff;
|
|
xGreen += (inGreen - xGreen) * cutoff;
|
|
xBlue += (inBlue - xBlue) * cutoff;
|
|
}
|
|
}
|
|
|
|
if(y == 0) {
|
|
outAlpha = xAlpha;
|
|
outRed = xRed;
|
|
outGreen = xGreen;
|
|
outBlue = xBlue;
|
|
} else {
|
|
outAlpha = yAlpha[x] + (xAlpha - yAlpha[x]) * cutoff;
|
|
if(xAlpha < 1) {
|
|
yAlphaWait[x] = true;
|
|
outRed = yRed[x];
|
|
outGreen = yGreen[x];
|
|
outBlue = yBlue[x];
|
|
} else {
|
|
yAlphaWait[x] = false;
|
|
outRed = yRed[x] + (xRed - yRed[x]) * cutoff;
|
|
outGreen = yGreen[x] + (xGreen - yGreen[x]) * cutoff;
|
|
outBlue = yBlue[x] + (xBlue - yBlue[x]) * cutoff;
|
|
}
|
|
}
|
|
|
|
yAlpha[x] = outAlpha;
|
|
yRed[x] = outRed;
|
|
yGreen[x] = outGreen;
|
|
yBlue[x] = outBlue;
|
|
|
|
if(++x == width) {
|
|
x = 0;
|
|
++y;
|
|
}
|
|
|
|
return Pixels.pack((int) outAlpha, (int) outRed, (int) outGreen, (int) outBlue);
|
|
}
|
|
|
|
public int processPass2Pixel(int pixel) {
|
|
return processPass1Pixel(pixel);
|
|
}
|
|
|
|
public boolean isPass1ReadOnly() {
|
|
return false;
|
|
}
|
|
|
|
public boolean hasPass2() {
|
|
return true;
|
|
}
|
|
|
|
public boolean isPass2Reversed() {
|
|
return true;
|
|
}
|
|
|
|
public void startPass1() {
|
|
x = 0;
|
|
y = 0;
|
|
xAlpha = 0;
|
|
xRed = 0;
|
|
xGreen = 0;
|
|
xBlue = 0;
|
|
yAlpha = new float[width];
|
|
yRed = new float[width];
|
|
yGreen = new float[width];
|
|
yBlue = new float[width];
|
|
xAlphaWait = false;
|
|
yAlphaWait = new boolean[width];
|
|
}
|
|
|
|
public void startPass2() {
|
|
startPass1();
|
|
}
|
|
}
|
|
|