473,387 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

fast alpha blending with 565RGB


Hi,

I have this code that blends 2 pixels, but it's not really fast. Can
someone help me speeding it up?

#define GETR(a) (unsigned char)(((((a)>>11)&31)*255)/31)
#define GETG(a) (unsigned char)(((((a)>5)&63)*255)/63)
#define GETB(a) (unsigned char)(((((a) )&31)*255)/31)

inline unsigned short PackColor(unsigned int r, unsigned int g,
unsigned int b)
{
return ((unsigned short)(
(b>>3) + ((g>>2)<<5) + ((r>>3)<<11)));
}
// Trying to simulate
// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//
// basically it's just
// red_destination =
// max(0xff,
// alpha*red_destination
// + (1-alpha)*red_source
// );
//
// pDest = pointer to destination pixel
// colsrc = color of source pixel
// alpha = alpha value (0...255 instead of 0.0f...1.0f)

static void BlendPixelSub(unsigned short* pDest, unsigned short
colsrc)
{
#define AONE(a) ((unsigned short)( ((a)*one_minus_alpha) >>8))
#define ONE(a) ((unsigned short)( ((a*alpha) >>8))

register unsigned long r,g,b, r2, g2, b2;
unsigned int one_minus_alpha = 255-alpha;

// rip out rgb values (THIS IS SLOW)
r =GETR(*pDest);
g =GETG(*pDest);
b =GETB(*pDest);
r2=GETR(colsrc);
g2=GETG(colsrc);
b2=GETB(colsrc);

// alpha blend them
r=AONE(r) +ONE(r2);
g=AONE(g) +ONE(g2);
b=AONE(b) +ONE(b2);

// limit to 0xff
if(r>0xff)r=0xff; if(g>0xff)g=0xff; if(b>0xff)b=0xff;

// put back
*pDest = PackColor(r, g, b);
#undef AONE
#undef ONE
}

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
Feb 20 '07 #1
3 3605
On Feb 20, 11:45 am, "Gernot Frisch" <M...@Privacy.netwrote:
Hi,

I have this code that blends 2 pixels, but it's not really fast. Can
someone help me speeding it up?

#define GETR(a) (unsigned char)(((((a)>>11)&31)*255)/31)
#define GETG(a) (unsigned char)(((((a)>5)&63)*255)/63)
#define GETB(a) (unsigned char)(((((a) )&31)*255)/31)
Can't help you much, but I'd say that it's the multiplies with 255 and
divisions with 31 or 63 that takes time. The multiply could probably
be made faster by using shifting left 8 times and then subtracting the
value that was shifted left. Perhaps one could do something similar
with the divisions too.

Oh, and instead of using macros, use inline functions (don't know if I
got this one right):

unsigned char getR(unsigned short a)
{
unsigned int tmp1 = a >11;
tmp1 &= 31;
unsigned int tmp2 = tmp1 << 8;
tmp1 = tmp2 - tmp1;
return static_cast<unsigned char>(tmp1 / 61);
}

--
Erik Wikström

Feb 20 '07 #2
Erik Wikström wrote:
On Feb 20, 11:45 am, "Gernot Frisch" <M...@Privacy.netwrote:
>Hi,

I have this code that blends 2 pixels, but it's not really fast. Can
someone help me speeding it up?

#define GETR(a) (unsigned char)(((((a)>>11)&31)*255)/31)
#define GETG(a) (unsigned char)(((((a)>5)&63)*255)/63)
#define GETB(a) (unsigned char)(((((a) )&31)*255)/31)

Can't help you much, but I'd say that it's the multiplies with 255 and
divisions with 31 or 63 that takes time. The multiply could probably
be made faster by using shifting left 8 times and then subtracting the
value that was shifted left. Perhaps one could do something similar
with the divisions too.

Oh, and instead of using macros, use inline functions (don't know if I
got this one right):

unsigned char getR(unsigned short a)
{
unsigned int tmp1 = a >11;
tmp1 &= 31;
unsigned int tmp2 = tmp1 << 8;
tmp1 = tmp2 - tmp1;
return static_cast<unsigned char>(tmp1 / 61);
}
Or it could all just be done in the FP unit... Impossible to tell for
sure without measuring. Also, possibly getting all three values at once
can end up being faster than getting them all separately...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 20 '07 #3
Also, possibly getting all three values at once
can end up being faster than getting them all separately...

That was it! Thank you.

#define RED ((0x00ff >3)<<SHIFTRED)
#define GREEN ((0x00ff >LEFTGREEN)<<5)
#define BLUE ((0x00ff >3))

// -------------------------------------------------------- //
// BlendPixel - Add
// -------------------------------------------------------- //
inline unsigned int _blend_mix(unsigned int a, unsigned int b,
unsigned int lim)
{a+=b; return a>lim?lim:a&lim;}

static void BlendPixelAdd(unsigned short* pDest, unsigned short
colsrc)
{
// alpha_blending = 1 .. 255
register unsigned int alpha = (unsigned int)alpha_blending;
#define ONE(a) ((unsigned int)(a))
#define AONE(a) (((unsigned int)(a)*alpha) >>8)
*pDest = (unsigned short)
(
(_blend_mix(ONE(*pDest & RED ), AONE(colsrc & RED ), RED ))
|(_blend_mix(ONE(*pDest & GREEN), AONE(colsrc & GREEN), GREEN ))
|(_blend_mix(ONE(*pDest & BLUE ), AONE(colsrc & BLUE ), BLUE ))
);
#undef ONE
#undef AONE
}

// -------------------------------------------------------- //
// BlendPixel - Sub
// -------------------------------------------------------- //
static void BlendPixelSub(unsigned short* pDest, unsigned short
colsrc)
{
// alpha_blending = -1 .. -255
register unsigned int one_minus_alpha = 256+alpha_blending,
alpha = -alpha_blending;
#define ONE(a) ( ((a)*alpha) >>8)
#define AONE(a) ( ((a)*one_minus_alpha) >>8)

*pDest = ((AONE(*pDest & RED ) + ONE(colsrc & RED ) ) & RED )
|((AONE(*pDest & GREEN) + ONE(colsrc & GREEN) ) & GREEN )
|((AONE(*pDest & BLUE ) + ONE(colsrc & BLUE ) ) & BLUE );

#undef ONE
#undef AONE
}
Feb 21 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Philippe Meunier | last post by:
Hi, I am using the .NET toolbar with VB.NET 2003. My toolbar uses icons images that are actually PNG files and uses alpha blending on it. So to make alpha blending work correctly with the...
3
by: bissatch | last post by:
Hi, Is it possible to set the alpha setting of an object (i.e. image) using CSS. I am aware that this can be done in IE only but I would rather use something more standard like CSS that can be...
0
by: Kathryn Doss | last post by:
<html> <body> <div style="width:100%;height:400px;"> <table style="position:relative;z-index:0;" width="100%" height="100%" border="0" cellpadding="20" cellspacing="0"> <tr> <td...
6
by: Renato | last post by:
Hi, I have seen the board described in this link. http://www.seventech.it/english/merlinoboard/2.php I think that its a good solution to realize an embedded system with VGA output, in that cases...
3
by: instruo | last post by:
I'm using the System.Drawing.Bitmap class for loading a 32-bit bmp file which includes an alpha channel. The problem is, when it gets loaded (just using the Bitmap(string filename) constructor),...
18
by: Andrew Christiansen | last post by:
Hey all. The images I create in photoshop with semi-transparent pixels (for instance in Photoshop text with a dropshadow with a transparent canvas) I've been saving in PNG format and then using...
2
by: David | last post by:
Hello... i want to draw on my form PNG file with alpha-blended transparency. so i use this code: Graphics canvas = e.Graphics; Image Image = Image.FromFile(@".\Dragon.png"); ...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
7
by: dave | last post by:
Hi All. I've been formulating in my head a simple image editor. I actually started prototyping is some time ago in Java, but am liking Python more and more. My editor will be nowhere near the level...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.