说明
以下的所有代码针对BGR像素排列的渲染缓存
灰度化代码
void GrayScaleProcess(unsigned char* pBuf, int width, int height)
{
for (int i=0; i<height; i++)
{
for (int j=0; j<width;j=j+1)
{
unsigned char* p = pBuf;
p = p + i*width*3+j*3;
unsigned char b = *(p);
unsigned char g = *(p+1);
unsigned char r = *(p+2);
unsigned char gray = 0.3*r+0.59*g+0.11*b;
*(p) = gray;
*(p+1) = gray;
*(p+2) = gray;
}
}
}
高斯滤波代码
void GaussianFilter (unsigned char* corrupted, unsigned char* smooth, int width, int height)
{
int templates[25] = {
1, 4, 7, 4, 1,
4, 16, 26, 16, 4,
7, 26, 41, 26, 7,
4, 16, 26, 16, 4,
1, 4, 7, 4, 1 };
memcpy ( smooth, corrupted, width*height*3*sizeof(unsigned char) );
for (int j=2;j<height-2;j++)
{
for (int i=6;i<width*3;i=i+3)
{
int sum = 0;
int index = 0;
for ( int m=j-2; m<j+3; m++)
{
for (int n=i-6; n<=i+6; n=n+3)
{
sum += corrupted [ m*width*3 + n] * templates[index] ;
index++;
}
}
sum /= 273;
if (sum > 255)
sum = 255;
smooth [ j*width*3+i ] = sum;
smooth [ j*width*3+i+1 ] = sum;
smooth [ j*width*3+i+2 ] = sum;
}
}
}
在AGG中的源代码效果对比
void GaussianFilter (unsigned char* corrupted, unsigned char* smooth, int width, int height)
{
int templates[25] = {
1, 4, 7, 4, 1,
4, 16, 26, 16, 4,
7, 26, 41, 26, 7,
4, 16, 26, 16, 4,
1, 4, 7, 4, 1 };
memcpy ( smooth, corrupted, width*height*3*sizeof(unsigned char) );
for (int j=2;j<height-2;j++)
{
for (int i=6;i<width*3;i=i+3)
{
int sum = 0;
int index = 0;
for ( int m=j-2; m<j+3; m++)
{
for (int n=i-6; n<=i+6; n=n+3)
{
sum += corrupted [ m*width*3 + n] * templates[index] ;
index++;
}
}
sum /= 273;
if (sum > 255)
sum = 255;
smooth [ j*width*3+i ] = sum;
smooth [ j*width*3+i+1 ] = sum;
smooth [ j*width*3+i+2 ] = sum;
}
}
}
void GrayScaleProcess(unsigned char* pBuf, int width, int height)
{
for (int i=0; i<height; i++)
{
for (int j=0; j<width;j=j+1)
{
unsigned char* p = pBuf;
p = p + i*width*3+j*3;
unsigned char b = *(p);
unsigned char g = *(p+1);
unsigned char r = *(p+2);
unsigned char gray = 0.3*r+0.59*g+0.11*b;
*(p) = gray;
*(p+1) = gray;
*(p+2) = gray;
}
}
}
void GaussianFilterTest()
{
agg::rendering_buffer &rbuf = rbuf_window();
agg::pixfmt_bgr24 pixf(rbuf);
typedef agg::renderer_base<agg::pixfmt_bgr24> renderer_base_type;
renderer_base_type renb(pixf);
agg::rasterizer_scanline_aa<> ras;
agg::scanline_u8 sl;
ren_bas.clear(agg::rgba8(255,255,255));
agg::ellipse ell(100,100,150,150);
agg::pixel_map pm_img;
if(!pm_img.load_from_bmp("E:/docs/agg2/beauty.bmp")) return;
agg::rendering_buffer rbuf_img(pm_img.buf(), pm_img.width(), pm_img.height(), -pm_img.stride());
agg::pixfmt_bgr24 pixf_img(rbuf_img);
rbuf.copy_from(rbuf_img);
GrayScaleProcess(rbuf.buf(), rbuf.width(), rbuf.height());
unsigned char* smooth = (unsigned char*)malloc(3600*1058*3);
int num = sizeof(smooth);
memset(smooth, 0x00, 3600*1058*3);
GaussianFilter(pm_img.buf(), smooth, pm_img.width(), pm_img.height());
unsigned char* bufPtr = rbuf.buf();
int smoothPtr=0;
for (int i=pm_img.height(); i<2*pm_img.height(); i++)
{
unsigned char* curHorizenPtr = bufPtr + i*rbuf.width()*3;
for (int j=0; j<pm_img.width()*3; j++)
{
*curHorizenPtr = smooth[smoothPtr];
curHorizenPtr++;
smoothPtr++;
}
}
::free(smooth);
return;
}