473,651 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)>>1 1)&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(unsig ned 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_SR C_ALPHA);
//
// basically it's just
// red_destination =
// max(0xff,
// alpha*red_desti nation
// + (1-alpha)*red_sour ce
// );
//
// pDest = pointer to destination pixel
// colsrc = color of source pixel
// alpha = alpha value (0...255 instead of 0.0f...1.0f)

static void BlendPixelSub(u nsigned 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=0xf f; if(g>0xff)g=0xf f; if(b>0xff)b=0xf f;

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

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%c gl%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 3620
On Feb 20, 11:45 am, "Gernot Frisch" <M...@Privacy.n etwrote:
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)>>1 1)&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<uns igned char>(tmp1 / 61);
}

--
Erik Wikström

Feb 20 '07 #2
Erik Wikström wrote:
On Feb 20, 11:45 am, "Gernot Frisch" <M...@Privacy.n etwrote:
>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)>>1 1)&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<uns igned 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(unsi gned int a, unsigned int b,
unsigned int lim)
{a+=b; return a>lim?lim:a&lim ;}

static void BlendPixelAdd(u nsigned short* pDest, unsigned short
colsrc)
{
// alpha_blending = 1 .. 255
register unsigned int alpha = (unsigned int)alpha_blend ing;
#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(ON E(*pDest & GREEN), AONE(colsrc & GREEN), GREEN ))
|(_blend_mix(ON E(*pDest & BLUE ), AONE(colsrc & BLUE ), BLUE ))
);
#undef ONE
#undef AONE
}

// -------------------------------------------------------- //
// BlendPixel - Sub
// -------------------------------------------------------- //
static void BlendPixelSub(u nsigned short* pDest, unsigned short
colsrc)
{
// alpha_blending = -1 .. -255
register unsigned int one_minus_alpha = 256+alpha_blend ing,
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
3529
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 Toolbar and the Imagelist I had to use a tricky method with API calls that is described like this by a MS MVP : 1) Use v6 of the common controls (have a manifest) 2) Load the PNG using GDI+
3
4651
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 supported in all browsers. Cheers Burnsy
0
1027
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 valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="64"><table width="64" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF"> <tr> <td>&nbsp;</td>
6
326
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 where, to use the PC, is not convenient. I have seen that it's available also a dev kit to develop the code in C++ and it is not necessary to use an operating system. How is it possible to work with a system without operating system? You thing...
3
17371
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), it doesn't bother bringing the alpha along with it. All of the pixels just show "255" as their alpha value and Bitmap.IsAlphaPixelFormat() returns false. I know that the alpha does exist physically and has worked with this exact image in other...
18
7434
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 them in my VB.NET app using the picturebox control. I was really happy when I learned that you could use PNG images in VB.NET so your images could blend in with the form's background color. But, is PNG the ideal image format to use with these...
2
2983
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"); canvas.DrawImage(Image, 0, 0, Image.Size.Width, Image.Size.Height);
95
5040
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 ____________________________________ | | | ------------------ | | | BUTTON | | | ...
7
3676
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 of Gimp/ Photoshop, but I do need fast pixel level control and display. For instance, that means no automatic anti-aliasing and that I will be implementing my own line drawing algorithms. I've got the high level architectual aspects of my...
0
8349
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8695
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8460
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8576
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7296
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4281
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2696
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.