图片模糊算法有均值模糊和高斯模糊,均值模糊快速但效果不如高斯,高斯模糊效果好但效率慢。
一种快速模糊算法:
算法取自网上,速度很快,更改成支持32位图,alpha通道也进行了计算
/*
* Super Fast Blur v1.1
* Original author: Mario Klingemann (C++ version)
* Original address: http://incubator.quasimondo.com/processing/superfastblur.pde
* C version and 32bit image support in here
*/
void SuperFastBlur(unsigned char *pix, int w, int h, int radius)
{
int div;
int wm, hm, wh;
int *vMIN, *vMAX;
unsigned char *r, *g, *b,*a, *dv;
int rsum, gsum, bsum,asum, x, y, i, p, p1, p2, yp, yi, yw;
if (radius < 1) return;
wm = w - 1;
hm = h - 1;
wh = w * h;
div = radius + radius + 1;
vMIN = (int *)malloc(sizeof(int)* max(w, h));
vMAX = (int *)malloc(sizeof(int)* max(w, h));
r = (unsigned char *)malloc(sizeof(unsigned char)* wh);
g = (unsigned char *)malloc(sizeof(unsigned char)* wh);
b = (unsigned char *)malloc(sizeof(unsigned char)* wh);
a = (unsigned char *)malloc(sizeof(unsigned char)* wh);
dv = (unsigned char *)malloc(sizeof(unsigned char)* 256 * div);
for (i = 0; i < 256 * div; i++)
dv[i] = (i / div);
yw = yi = 0;
for (y = 0; y < h; y++)
{
rsum = gsum = bsum = asum=0;
for (i = -radius; i <= radius; i++)
{
p = (yi + min(wm, max(i, 0))) * 4;
bsum += pix[p];
gsum += pix[p + 1];
rsum += pix[p + 2];
asum += pix[p + 3];
}
for (x = 0; x < w; x++)
{
r[yi] = dv[rsum];
g[yi] = dv[gsum];
b[yi] = dv[bsum];
a[yi] = dv[asum];
if (y == 0)
{
vMIN[x] = min(x + radius + 1, wm);
vMAX[x] = max(x - radius, 0);
}
p1 = (yw + vMIN[x]) * 4;
p2 = (yw + vMAX[x]) * 4;
bsum += pix[p1] - pix[p2];
gsum += pix[p1 + 1] - pix[p2 + 1];
rsum += pix[p1 + 2] - pix[p2 + 2];
asum += pix[p1 + 3] - pix[p2 + 3];
yi++;
}
yw += w;
}
for (x = 0; x < w; x++)
{
rsum = gsum = bsum = asum=0;
yp = -radius * w;
for (i = -radius; i <= radius; i++)
{
yi = max(0, yp) + x;
rsum += r[yi];
gsum += g[yi];
bsum += b[yi];
asum += a[yi];
yp += w;
}
yi = x;
for (y = 0; y < h; y++)
{
pix[yi * 4] = dv[bsum];
pix[yi * 4 + 1] = dv[gsum];
pix[yi * 4 + 2] = dv[rsum];
pix[yi * 4 + 3] = dv[asum];
if (x == 0)
{
vMIN[y] = min(y + radius + 1, hm) * w;
vMAX[y] = max(y - radius, 0) * w;
}
p1 = x + vMIN[y];
p2 = x + vMAX[y];
rsum += r[p1] - r[p2];
gsum += g[p1] - g[p2];
bsum += b[p1] - b[p2];
asum += a[p1] - a[p2];
yi += w;
}
}
free(r);
free(g);
free(b);
free(a);
free(vMIN);
free(vMAX);
free(dv);
}