PocketMatrix forums

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

Blur effect help!

Hi,

I am trying to write a blur effect in a pixel shader but they resulting image is too dark and very green. Can someone help me understand what is going on?

Btw, I took the code from a basic forum. I don't understand it well. Here is the link to the article:

http://www.blitzbasic.com/codearcs/code ... p?code=837

Code:
int r = (s & Frog::RED_MASK) >> Frog::RED_SHIFT;
int g = (s & Frog::GREEN_MASK) >> Frog::GREEN_SHIFT;
int b = (s & Frog::BLUE_MASK) >> Frog::BLUE_SHIFT;

desr = r * (1-blurlevel) + desr * (blurlevel);
desg = g * (1-blurlevel) + desg * (blurlevel);
desb = b * (1-blurlevel) + desb * (blurlevel);

d = Frog::Color(desr, desg, desb);


The bluring occurs, it just the darkness and greenish that bugs me.

I'd like to make a NEGATIVE and GRAYSCALE filter after that, if someone can help me on that, it would be really appreciated as well.
I thought I included a couple of shaders in the last PF release I did, including a greyscale shader, but in any case, here's the greyscale shader I have laying around... No warranty though :) ... and I don't know if this is compatible with the latest PF either, I assume it is without very little change though...
Code:

struct GreyscaleShader : public PixelShader {
Pixel operator() (Pixel src, Pixel dest) const {
register int R = src & RED_MASK;
register int G = src & GREEN_MASK;
register int B = src & BLUE_MASK;
R = R >> RED_SHIFT;
G = G >> (GREEN_SHIFT + 1);
B = B >> BLUE_SHIFT;
register int finalColor = (R + G + B) / 3;
return (finalColor << RED_SHIFT | finalColor << (GREEN_SHIFT + 1) | finalColor << BLUE_SHIFT);
}
};
Any local filter, like "blur" needs several pixels to mix them together. Of course it's possible to use only vertical or horisontal part(or diagonal maybe), so you will need at least 2 pixels to mix. But how can your "blur" actually work, if pixel shader only allows to work with one single pixel?
The desr, desg and desb are stored globally not within the shader. This one should be an horizontal blur providing the shader is going through pixels in an horizontal method.

fmazetti: Thank you very much for the shader it works great.

I tried doing a negative one, does it look okay to you guys?

Code:
int r = (s & Frog::RED_MASK) >> Frog::RED_SHIFT;
int g = (s & Frog::GREEN_MASK) >> Frog::GREEN_SHIFT;
int b = (s & Frog::BLUE_MASK) >> Frog::BLUE_SHIFT;

//int av = 255 - (r + g + b) / 3;

d = Frog::Color(255 - r, 255 - g, 255 - b);
Without seeing it in action, your negative code strikes me as odd... If it works it works and I just didn't understand it, but it seems like you'd get just an odd coloration. I mean, if you have RGB 120, 200, 80 to start with, you'll end up with 135, 55, 175 (if my mental subtraction is right)... Is that really a negative version of the source color?

When I think negative image I think of an image formed by taking a source image, converting it to black and white and then inverting every pixel. If that's the case, it's basically a two-step process, which you can do in the shader just fine, although I'm not really sure how to convert to black and white... I guess it could be as simple as any pixel that isn't your transparent color you make black, otherwise it's white, then you just set all black pixels to white, something like this:

Code:

int r = (s & Frog::RED_MASK) >> Frog::RED_SHIFT;
int g = (s & Frog::GREEN_MASK) >> Frog::GREEN_SHIFT;
int b = (s & Frog::BLUE_MASK) >> Frog::BLUE_SHIFT;

// Assume transparent color is 255,128,255
if (r == 255 && g == 128 && b == 255) {
r = 255; g = 255; b = 255;
} else {
r = 0; g = 0; b = 0;
{

if (r == 0 && g == 0 && b == 0) {
d = Frog::Color(255, 255, 255);
} else {
d = Frog::Color(0, 0, 0);
}

return d;

Or, alternatively, I am wrong and your shader does exactly what you intend ;)
kornalius wrote:
Code:
int r = (s & Frog::RED_MASK) >> Frog::RED_SHIFT;
int g = (s & Frog::GREEN_MASK) >> Frog::GREEN_SHIFT;
int b = (s & Frog::BLUE_MASK) >> Frog::BLUE_SHIFT;


That wont give you 0-255 values for RGB.

The range for red and blue is 0..31, and for green it is 0..63. Remember that the pixels are in R5G6B5 format, not R8G8B8.

Change your last line to this:

Code:

d = ((31-r) << RED_SHIFT) | ((63-g) << GREEN_SHIFT) | ((31-b) << BLUE_SHIFT);
Thanks Thierry I will give a try.

Btw, I'd like to finish this blur effect. Can anyone give it a try? The greyscale and negative are working, I just need to finish the blur.

I will post the effects code here to share with the others as well.
Kornalius,

Did you figure out the blur effect? Does anyone else have any cool PixelShaders that they would love to share?
Hi!
I have the same problem with colors. They are green and black :( I tried to make bilinear filtering to shrink surface without bugs. Here is the result

Code:
DisplayDevice* display = GetDisplay();
Surface* m_buffer = display->CreateSurface(160,120);
Surface::LockInfo buffer;
Surface::LockInfo buffer2;
int offset = 0;
display->GetBackBuffer()->Lock( buffer );
m_buffer->Lock(buffer2);
// initialize vars
int dy = buffer.pitch >> 1;
int dy2 = buffer2.pitch >> 1;

fcolor a,b,c;
Pixel res;
int width = display->GetWidth();
int height = display->GetHeight();

for( int y = 0; y < height-1; ++y )
{
for( int x = 0; x < width-1; ++x )
{

a.r = ((buffer.pixels[y * dy + x] & RED_MASK)>>RED_SHIFT)+(((buffer.pixels[y * dy + x+1] & RED_MASK)>>RED_SHIFT) - ((buffer.pixels[y * dy + x] & RED_MASK)>>RED_SHIFT))* 0.5;
a.g = ((buffer.pixels[y * dy + x] & GREEN_MASK)>>GREEN_SHIFT)+(((buffer.pixels[y * dy + x+1] & GREEN_MASK)>>GREEN_SHIFT)-((buffer.pixels[y * dy + x] & GREEN_MASK)>>GREEN_SHIFT)) * 0.5;
a.b = ((buffer.pixels[y * dy + x] & BLUE_MASK)>>BLUE_SHIFT)+(((buffer.pixels[y * dy + x+1] & BLUE_MASK)>>BLUE_SHIFT)-((buffer.pixels[y * dy + x] & BLUE_MASK)>>BLUE_SHIFT)) * 0.5;

b.r = ((buffer.pixels[(y+1) * dy + x] & RED_MASK)>>RED_SHIFT)+(((buffer.pixels[(y+1) * dy + x+1] & RED_MASK)>>RED_SHIFT)-((buffer.pixels[(y+1) * dy + x] & RED_MASK)>>RED_SHIFT)) * 0.5;
b.g = ((buffer.pixels[(y+1) * dy + x] & GREEN_MASK)>>GREEN_SHIFT)+(((buffer.pixels[(y+1) * dy + x+1] & GREEN_MASK)>>GREEN_SHIFT)-((buffer.pixels[(y+1) * dy + x] & GREEN_MASK)>>GREEN_SHIFT)) * 0.5;
b.b = ((buffer.pixels[(y+1) * dy + x] & BLUE_MASK)>>BLUE_SHIFT)+(((buffer.pixels[(y+1) * dy + x+1] & BLUE_MASK)>>BLUE_SHIFT)-((buffer.pixels[(y+1) * dy + x] & BLUE_MASK)>>BLUE_SHIFT)) * 0.5;

c.r = a.r + b.r*0.5;
c.g = a.g + b.g*0.5;
c.b = a.b + b.b*0.5;

res = Color((int)c.r,(int)c.g,(int)c.b);
buffer2.pixels[y/2 * dy2 + x/2] = res;
}
}


display->GetBackBuffer()->Unlock();
m_buffer->Unlock();
display->Blit(80,60,m_buffer);
delete m_buffer;

Where is my fault? :)
Attachments:
Blit.rar (55 KB)
This is just a guess... but isn't buffer.pixels an unsigned int16?

so, when you calc your difference, it could give you a big number instead of the negative delta you expect.

Why not go ahead and put the RGB values into your struct before the formulas (to force the type as well as make it readable).

And it seems like the formulas not right. Are you trying to average 4 pixels?
Yes, formulas intend to average 4 pixels. I used formulas from russian article about bilinear filtration.

c1,c2,c3,c4 - pixels

a = c1 + (c3-c1)*deltax
b = c2 + (c4-c2)*deltax

newc=a+b*deltay

Are they wrong?
newc=a+(b-a)*deltay

instead of
newc=a+b*deltay
?

But unless it is some sort of weighted average though, I'd just add them all up and shift right by 2...