PocketMatrix forums

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

Colored blit / Color Shader

Please, help!

I need colored blit, but Iam not able make corect shader for this. I know how to make dark shader, but I can not find way to create shader whic colorize blited surface to any color.

I need to set R, G, B values and blit bitmap. This bitmap will be colorized by rgb (origina white color will be equal to rgb and black color stay black. Other colors colorize.

Please, how I can do this?
Yes, this work, but this is not what I need.

If I set in your example shade variable to Color(255, 255, 255), original color is "lighted" by white. But included black color in original bitmap - balck is not black but light gray.

But I need just colorize, not highlite. I need beahavior in which when I set color (255,255,255), that original picture stay unmodified. When I set (100, 0, 0) that picture is colored to dark red (white = dark red, black stay black), etc.
It sounds like you need to factor out the rgb of the source pixels, multiply by myColor's rgb, and put back together...

Code:

struct MyShader : public PixelShader
{
Pixel operator() (Pixel src, Pixel dest) const
{

int sr = (src & RED_MASK) >> RED_SHIFT;
int sg = (src & GREEN_MASK) >> GREEN_SHIFT;
int sb = (src & BLUE_MASK) >> BLUE_SHIFT;

int cr = (myColor & RED_MASK) >> RED_SHIFT;
int cg = (myColor & GREEN_MASK) >> GREEN_SHIFT;
int cb = (myColor & BLUE_MASK) >> BLUE_SHIFT;


sr = sr*cr/255;
sg = sg*cg/255;
sb = sb*cb/255;

return ( ((sr&RED_MASK)<<RED_SHIFT) | ((sg&GREEN_MASK)<<GREEN_SHIFT) | ((sb&BLUE_MASK)<<BLUE_SHIFT) );
}
};
Yes, something like this, but this dosn't work :cry:
Well, there's some errors in that code...

1. RGB values are not 0-255, so the '/255' should be values that match the 565 pixel format.
2. I think the mask and shift are backwards in the return statement.
I try make it by myself and this solution works.
I know that it isn't realy clear, but it works as I need...

If you help me improve this to much more sophisticated code, I'll be happy :lol:

Code:
//Color Shader
struct ColorShader : public PixelShader
{
int m_R;
int m_G;
int m_B;

void SetColor(Pixel color)
{
m_R = (color & RED_MASK) >> 8; // red
m_G = (color & GREEN_MASK) >> 3; // green
m_B = (color & BLUE_MASK) << 3; // blue
}

void SetColor(int r, int g, int b)
{
Pixel color = Color(r, g, b);
SetColor(color);
}

Pixel operator() (Pixel src, Pixel dest) const
{
register int r = (src & RED_MASK) >> 8; // red
register int g = (src & GREEN_MASK) >> 3; // green
register int b = (src & BLUE_MASK) << 3; // blue

r -= (255 - m_R);
g -= (255 - m_G);
b -= (255 - m_B);

if (r < 0) r = 0;
if (g < 0) g = 0;
if (b < 0) b = 0;

return Color(r, g, b);
}
};
I like the way you put the static color variable in the shader with SetColor... It never occured to me to do this.

I guess the way you did the colorizing vs. the way I was doing it is similar to the differences in graphics program layer operations... additive, subractive, burn, dodge, etc...

It would be nice to work up a package of shaders that correspond to those operations.
fast_rx wrote:I like the way you put the static color variable in the shader with SetColor... It never occured to me to do this.

I guess the way you did the colorizing vs. the way I was doing it is similar to the differences in graphics program layer operations... additive, subractive, burn, dodge, etc...

It would be nice to work up a package of shaders that correspond to those operations.

I think it's pretty simple, actually. The really nice thing would be something faster than extracting each component, but I can't figure anything except for these two:

Code:
struct AdditiveShader: public PixelShader
{


Pixel operator() (Pixel src, Pixel dest) const
{

return (src | dest);

}
};

extern AdditiveShader additiveShader;

struct MultiplicativeShader: public PixelShader
{


Pixel operator() (Pixel src, Pixel dest) const
{

return (src & dest);

}
};

extern MultiplicativeShader multiplicativeShader;


They are obvious, but I didn't get it first time.
Btw, fast_rx, you did mentioned that you've made a special blit functions for these two. Is it considerably faster than using this custom shaders?
Yes, I have an additive and subtractive that I built in to pf. I can run a benchmark on it to see if it's much faster than using a shader. I'll post the results later.

One thing about your optimization... Repeated additive blits using a color like 32,32,32 should get to white after 8 blits. Your optimization wouldn't.

The optimizations I'm using is something Jinks wrote about. Basically moving the green component left so you have a 32bit number that represents xGxRxB. Then you can do operations without overflowing into the other components.
Jaguard, I just realized I was getting confused between my additive blit vs. this colorizing-additive shader...

You're right, I think that those optimizations would be right for the colorizing.

Ok, so the results of using a shader vs. rolling-your-own PF function.

Shader: 100.8 blits/33mS
Custom Function: 125.0 blits/33mS

So, the custom function wins... which is what I would expect, since the shader has to be called for every pixel. The function call overhead adds up.

Here is the shader I was using:
Code:

struct AdditiveShaderdef : public PixelShader
{
Pixel operator() (Pixel src, Pixel dest) const
{

unsigned short pd = (dest & 63454) >> 1; //mask off LSBs of RGB
unsigned short ps = (src & 63454) >> 1;

pd += ps;

if(pd & 0x8000) //red overflow
pd |= (0xF800);// >> 1); //highest red
if(pd & (0x400)) //green overflow
pd |= (0x7E0);// >> 1); //highest green
if(pd & ( 0x10 )) //blue overflow
pd |= ( 0x1F); //highest blue


return ( pd<<1 );
}
};


I was wrong about using the xGxRxB technique, but it could have been used.
fast_rx wrote:
One thing about your optimization... Repeated additive blits using a color like 32,32,32 should get to white after 8 blits. Your optimization wouldn't.


Yes, you're right. But it's fast and sometimes works just as needed. For example, I have particles looking like this:
Code:

11111
15751
17971
15751
11111


Nice technique! I will think what use I can get out of it, but I like it.

And the results are also nice, thank you.