PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Fast 16 bit box filter blurring?

Hello community,

for some fancy FX routines I lately tried to come up with a fast 16 bit 2d box filter blur. After some research I ended up with this article:
http://www.gamasutra.com/features/20010209/evans_pfv.htm
Some source codehere...
It explains how to achieve a fast 2d box blur by applying a horizontal and a vertical blur consecutively.
So much for the theory. I wrote a straight forward version using the same technique for each color component and I must confess it's already
painfully slow on my 206Mhz ARM CPU, probably for caching reasons.

Is anyone interested in helping me optimizing the provided technique to 16 bits?
I really need some fancy ideas.
How fast do you need it to be ?

And what is the required blur quality ?

About caching issue, I once implemented a fullscreen crossfade in asm, it was running in less than 8ms (two 240 * 320 * 16 screens)

A fast blur could help to create a bloom effect, could be nice :)
Thanks for the fast answer.

Well it should be done in realtime on a 320x240 picture as quickly as possible and the outcome of the filter should be somewhat like of the provided algorithm.
Of course it should be possible to specify different blur amounts without sacrificing too much speed.
(And I'd prefer not using any assembler to keep it as platform independent as possible)

About that bloom effect, I'm not sure what you mean by that, do you have any example pictures of it?

Cheers
xPeterx wrote:
About that bloom effect, I'm not sure what you mean by that, do you have any example pictures of it?


Well, check about any screenshot of Far Cry.

It's a quite simple post processing effect.

Simply blur the brightest pixels, each pass with a larger kernel.

(The real effect is a bit more complicated, but I think a nice 2D bloom could be created based on a fast blur routine)

How fast is your first implementation?
One way I see to create a blurring effect, is by sampling the image at intervals that are powers of 2. Then one can linearly interpolate the colors in between the pixels with gradients (basically generating a smoothed mosaic effect). Note that the gradients can be computed by a simple shifiting operation at the begining of each sample (provided we sample at intervals that are powers of 2), and in between it's just an addition. Nice thing is that you'll read less data, but the blur will be "dirty" and akin to the results in the "MIP-Mapping Abused" trick posted in your link.

Anyways motionblur and blur in general can improve the graphical appeal in many games, so I'd be interested in this as well.
StephC wrote:
It's a quite simple post processing effect.

Simply blur the brightest pixels, each pass with a larger kernel.

(The real effect is a bit more complicated, but I think a nice 2D bloom could be created based on a fast blur routine)

Oh yes sure I've seen it before, this could be indeed very nice, even in software.

As for the performance, I've not yet made any real performance tests (measuring any numbers) but simply try with the routine from the link I've provided, basically that's all I did (the same for r/g/b and see how fast it runs)
_Alex_ wrote:One way I see to create a blurring effect, is by sampling the image at intervals that are powers of 2. Then one can linearly interpolate the colors in between the pixels with gradients (basically generating a smoothed mosaic effect). Note that the gradients can be computed by a simple shifiting operation at the begining of each sample (provided we sample at intervals that are powers of 2), and in between it's just an addition. Nice thing is that you'll read less data, but the blur will be "dirty" and akin to the results in the "MIP-Mapping Abused" trick posted in your link.

Yeah I've tried that before but it simply looks too much like bilinear interpolation. Well in fact it is ;)
And of course you're very much limited when it comes to the amount of blur.
If you still have the code try second order interpolation that might smooth things out even more. Heck I'll do it if you don't mind posting the code for your bilinear interpolation.
_Alex_ wrote:If you still have the code try second order interpolation that might smooth things out even more. Heck I'll do it if you don't mind posting the code for your bilinear interpolation.

Sure, here you go:

Code:
#define RGB2SHORT(r,g,b) ((((r))<<11)+(((g))<<5)+((b)))

void Filter::BlurBiLerp(unsigned short* srcImage, int width, int height, unsigned short* tmpImage, int cellSizeShift)
{
int gWidth = width>>cellSizeShift;
int gHeight = height>>cellSizeShift;

int x,y,x2,y2;

unsigned short* ptr = tmpImage;
for (y = 0; y < gHeight; y++)
for (x = 0; x < gWidth; x++)
*ptr++ = srcImage[(y<<cellSizeShift)*width+(x<<cellSizeShift)];

for (y = 0; y < gHeight; y++)
for (x = 0; x < gWidth; x++)
{
int offset = y*gWidth+x;
int hOffs = 1;
int vOffs = gWidth;
if (x == gWidth-1)
hOffs = 0;
if (y == gHeight-1)
vOffs = 0;

unsigned int rgb1, rgb2, rgb3, rgb4;

rgb1 = tmpImage[offset];
rgb2 = tmpImage[offset+hOffs];
rgb3 = tmpImage[offset+vOffs+hOffs];
rgb4 = tmpImage[offset+vOffs];

int r1 = (rgb1>>11)<<16, g1 = (rgb1&0x7E0)<<11, b1 = (rgb1&0x1f)<<16;
int r2 = (rgb2>>11)<<16, g2 = (rgb2&0x7E0)<<11, b2 = (rgb2&0x1f)<<16;
int r3 = (rgb3>>11)<<16, g3 = (rgb3&0x7E0)<<11, b3 = (rgb3&0x1f)<<16;
int r4 = (rgb4>>11)<<16, g4 = (rgb4&0x7E0)<<11, b4 = (rgb4&0x1f)<<16;

int dr1 = (r4-r1)>>cellSizeShift;
int dr2 = (r3-r2)>>cellSizeShift;

int dg1 = (g4-g1)>>cellSizeShift;
int dg2 = (g3-g2)>>cellSizeShift;

int db1 = (b4-b1)>>cellSizeShift;
int db2 = (b3-b2)>>cellSizeShift;

ptr = srcImage+(y<<cellSizeShift)*width+(x<<cellSizeShift);

for (y2=0;y2<(1<<cellSizeShift);y2++)
{
int fr = (r2-r1)>>cellSizeShift;
int sr = r1;

int fg = (g2-g1)>>cellSizeShift;
int sg = g1;

int fb = (b2-b1)>>cellSizeShift;
int sb = b1;

for (x2=0;x2<(1<<cellSizeShift);x2++)
{
*ptr++ = RGB2SHORT(sr>>16,sg>>16,sb>>16);
sr+=fr;
sg+=fg;
sb+=fb;
}

ptr+=(1<<cellSizeShift);

r1+=dr1;
r2+=dr2;

g1+=dg1;
g2+=dg2;

b1+=db1;
b2+=db2;
}

}

}
It's untested and can still be optimized heavily, but you'll get an idea how it'll look like.