📄 gaussianblurfilter.java
字号:
// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3)
// Source File Name: GaussianBlurFilter.java
package se.southend.drops.pixelfilter;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
// Referenced classes of package se.southend.drops.pixelfilter:
// Filter
public class GaussianBlurFilter
implements Filter
{
public GaussianBlurFilter()
{
}
public GaussianBlurFilter(Image fromImage, int blurSize)
{
setBlurSize(blurSize);
setImage(fromImage);
}
public void setImage(Image fromImage)
{
if(fromImage == null)
{
return;
} else
{
int width = fromImage.getWidth();
int height = fromImage.getHeight();
int fromRGB[] = new int[width * height];
fromImage.getRGB(fromRGB, 0, width, 0, 0, width, height);
setRGB(fromRGB, width, height);
return;
}
}
public void setRGB(int fromRGB[], int width, int height)
{
this.width = width + blurSize * 2;
this.height = height + blurSize * 2;
rgb = new int[this.width * this.height];
tempRGB = new int[rgb.length];
for(int i = 0; i < rgb.length; i++)
rgb[i] = 0x7f7f7f;
for(int y = 0; y < height; y++)
{
int y_srcWidth = y * width;
int y_destWidth = (y + blurSize) * this.width;
for(int x = 0; x < width; x++)
rgb[x + blurSize + y_destWidth] = fromRGB[x + y_srcWidth];
}
}
public Image getImage()
{
return Image.createRGBImage(getRGB(), getWidth(), getHeight(), true);
}
public int[] getRGB()
{
return rgb;
}
public int getWidth()
{
return width;
}
public int getHeight()
{
return height;
}
public void setBlurSize(int blurSize)
{
this.blurSize = blurSize * 2 + 1;
}
public void render()
{
if(blurSize <= 3)
rgb = createBlur3x3(rgb, tempRGB, width, height);
else
if(blurSize <= 5)
rgb = createBlur5x5(rgb, tempRGB, width, height);
else
if(blurSize <= 7)
rgb = createBlur7x7(rgb, tempRGB, width, height);
else
createGaussianBlur(rgb, tempRGB, width, height, blurSize);
}
public void paintAt(Graphics graphics, int x, int y)
{
graphics.drawRGB(rgb, 0, width, x, y, width, height, true);
}
private static final int[] createBlur3x3(int src[], int temp[], int width, int height)
{
for(int y = 0; y < height; y++)
{
int y_width = y * width;
for(int x = 1; x < width - 1; x++)
temp[x + y_width] = (src[(x - 1) + y_width] >> 2 & 0x3f3f3f3f) + (src[x + y_width] >> 1 & 0x7f7f7f7f) + (src[x + 1 + y_width] >> 2 & 0x3f3f3f3f);
}
for(int y = 1; y < height - 1; y++)
{
int y_width = y * width;
for(int x = 0; x < width; x++)
src[x + y_width] = (temp[(x + y_width) - width] >> 2 & 0x3f3f3f3f) + (temp[x + y_width] >> 1 & 0x7f7f7f7f) + (temp[x + y_width + width] >> 2 & 0x3f3f3f3f);
}
return src;
}
private static final int[] createBlur5x5(int src[], int temp[], int width, int height)
{
for(int y = 0; y < height; y++)
{
int y_width = y * width;
for(int x = 2; x < width - 2; x++)
temp[x + y_width] = (src[(x - 2) + y_width] >> 4 & 0xf0f0f0f) + (src[(x - 1) + y_width] >> 2 & 0x3f3f3f3f) + (src[x + y_width] >> 2 & 0x3f3f3f3f) + (src[x + y_width] >> 3 & 0x1f1f1f1f) + (src[x + 1 + y_width] >> 2 & 0x3f3f3f3f) + (src[x + 2 + y_width] >> 4 & 0xf0f0f0f);
}
for(int y = 2; y < height - 2; y++)
{
int y_width = y * width;
for(int x = 0; x < width; x++)
src[x + y_width] = (temp[(x + y_width) - (width << 1)] >> 4 & 0xf0f0f0f) + (temp[(x + y_width) - width] >> 2 & 0x3f3f3f3f) + (temp[x + y_width] >> 2 & 0x3f3f3f3f) + (temp[x + y_width] >> 3 & 0x1f1f1f1f) + (temp[x + y_width + width] >> 2 & 0x3f3f3f3f) + (temp[x + y_width + (width << 1)] >> 4 & 0xf0f0f0f);
}
return src;
}
private static final int[] createBlur7x7(int src[], int temp[], int width, int height)
{
for(int y = 0; y < height; y++)
{
int y_width = y * width;
for(int x = 3; x < width - 3; x++)
temp[x + y_width] = (src[(x - 3) + y_width] >> 6 & 0x1010101) + (src[(x - 2) + y_width] >> 4 & 0xf0f0f0f) + (src[(x - 2) + y_width] >> 5 & 0x7070707) + (src[(x - 1) + y_width] >> 2 & 0x3f3f3f3f) + (src[x + y_width] >> 2 & 0x3f3f3f3f) + (src[x + y_width] >> 5 & 0x7070707) + (src[x + 1 + y_width] >> 2 & 0x3f3f3f3f) + (src[x + 2 + y_width] >> 4 & 0xf0f0f0f) + (src[x + 2 + y_width] >> 5 & 0x7070707) + (src[x + 3 + y_width] >> 6 & 0x1010101);
}
for(int y = 3; y < height - 3; y++)
{
int y_width = y * width;
for(int x = 0; x < width; x++)
src[x + y_width] = (temp[(x + y_width) - width * 3] >> 6 & 0x1010101) + (temp[(x + y_width) - (width << 1)] >> 4 & 0xf0f0f0f) + (temp[(x + y_width) - (width << 1)] >> 5 & 0x7070707) + (temp[(x + y_width) - width] >> 2 & 0x3f3f3f3f) + (temp[x + y_width] >> 2 & 0x3f3f3f3f) + (temp[x + y_width] >> 5 & 0x7070707) + (temp[x + y_width + width] >> 2 & 0x3f3f3f3f) + (temp[x + y_width + (width << 1)] >> 4 & 0xf0f0f0f) + (temp[x + y_width + (width << 1)] >> 5 & 0x7070707) + (temp[x + y_width + width * 3] >> 6 & 0x1010101);
}
return src;
}
private static final void createGaussianBlur(int src[], int temp[], int width, int height, int kernelSize)
{
int gaussCoeff[] = calcGaussCoefficients(kernelSize);
int kernelLog = kernelSize - 1;
int halfKernelSize = kernelSize >> 1;
int rightBoundary = (width + width) - 1;
int bottomBoundary = (height + height) - 1;
for(int y = 0; y < height; y++)
{
int y_width = y * width;
for(int x = 0; x < width; x++)
{
long rSum;
long gSum;
long bSum;
long aSum = rSum = gSum = bSum = 0L;
for(int k = 0; k < kernelSize; k++)
{
int xCol = (x - halfKernelSize) + k;
if(xCol < 0)
xCol = -xCol;
else
if(xCol >= width)
xCol = rightBoundary - xCol;
int color = src[xCol + y_width];
long coeff = gaussCoeff[k];
aSum += (long)(color >> 24 & 0xff) * coeff;
rSum += (long)(color >> 16 & 0xff) * coeff;
gSum += (long)(color >> 8 & 0xff) * coeff;
bSum += (long)(color & 0xff) * coeff;
}
temp[x + y_width] = (int)((aSum >> kernelLog) << 24 | (rSum >> kernelLog) << 16 | (gSum >> kernelLog) << 8 | bSum >> kernelLog);
}
}
for(int y = 0; y < height; y++)
{
int y_width = y * width;
for(int x = 0; x < width; x++)
{
long rSum;
long gSum;
long bSum;
long aSum = rSum = gSum = bSum = 0L;
for(int k = 0; k < kernelSize; k++)
{
int yCol = (y - halfKernelSize) + k;
if(yCol < 0)
yCol = -yCol;
else
if(yCol >= height)
yCol = bottomBoundary - yCol;
int color = temp[x + yCol * width];
long coeff = gaussCoeff[k];
aSum += (long)(color >> 24 & 0xff) * coeff;
rSum += (long)(color >> 16 & 0xff) * coeff;
gSum += (long)(color >> 8 & 0xff) * coeff;
bSum += (long)(color & 0xff) * coeff;
}
src[x + y_width] = (int)((aSum >> kernelLog) << 24 | (rSum >> kernelLog) << 16 | (gSum >> kernelLog) << 8 | bSum >> kernelLog);
}
}
}
private static final int[] calcGaussCoefficients(int size)
{
if(size < 2)
{
int coefficients[] = {
1
};
return coefficients;
}
int previous[] = calcGaussCoefficients(size - 1);
int coefficients[] = new int[size];
for(int i = 0; i < size; i++)
{
if(i == 0)
{
coefficients[i] = previous[i];
continue;
}
if(i == size - 1)
coefficients[i] = previous[i - 1];
else
coefficients[i] = previous[i] + previous[i - 1];
}
return coefficients;
}
protected int tempRGB[];
protected int rgb[];
protected int width;
protected int height;
protected int blurSize;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -