| | |
Alpha blend algorithm
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
This algorithm proved to be effective when two images are combined. The picture beneath (background) is seen if alpha component of fronth picture is less then 0xFF. If alpha of front picture (src in snippet) is zero then all we see is background picture. The format of colour is ARGB which means 32 bit variables are used to represent the color of pixels (off course alpha, red, green, blue are one bytes).
/* alpha blend routine */ unsigned int AlphaBlend(const unsigned int bg, const unsigned int src) { unsigned int a = src >> 24; /* alpha */ /* If source pixel is transparent, just return the background */ if (0 == a) return bg; /* alpha blending the source and background colors */ unsigned int rb = (((src & 0x00ff00ff) * a) + ((bg & 0x00ff00ff) * (0xff - a))) & 0xff00ff00; unsigned int g = (((src & 0x0000ff00) * a) + ((bg & 0x0000ff00) * (0xff - a))) & 0x00ff0000; return (src & 0xff000000) | ((rb | g) >> 8); }
0
•
•
•
•
any explanation for the code? what are those magic numbers (0x00ff00ff, 0xff00ff00, etc.) and why red and blue rb?
0
•
•
•
•
rb stands for red and blue. To get this component we must mask with 0x00ff00ff. Why?
Becouse the first 8 MSB bits are the alpha transparent component, next byte (or 8 bits) is the red component then the green component follows and finaly the blue component.
Thats why for g (green) variable the 0x0000ff00 mask is used. So the resultant picture is formed from rb, g and from alpha component of source pictures.
Becouse the first 8 MSB bits are the alpha transparent component, next byte (or 8 bits) is the red component then the green component follows and finaly the blue component.
Thats why for g (green) variable the 0x0000ff00 mask is used. So the resultant picture is formed from rb, g and from alpha component of source pictures.
0
•
•
•
•
andor,
What you're doing is basically pixel_out=(pixel_1*alpha+pixel_2*(255-alpha))/256. The denominator is changed from 255 to 256 because it's cheaper, but also it has a disadvantage. When alpha equals to 0xFF, pixel_out supposed to be equal to pixel_1 but what you get is pixel_1*255/256 which is a little small than pixel_1.
Actually I'm having the same problem and I need to implement alpha blending in hardware. Do you have a solution better than this?
Regards,
Jason.
What you're doing is basically pixel_out=(pixel_1*alpha+pixel_2*(255-alpha))/256. The denominator is changed from 255 to 256 because it's cheaper, but also it has a disadvantage. When alpha equals to 0xFF, pixel_out supposed to be equal to pixel_1 but what you get is pixel_1*255/256 which is a little small than pixel_1.
Actually I'm having the same problem and I need to implement alpha blending in hardware. Do you have a solution better than this?
Regards,
Jason.
Similar Threads
- perfect blend of gaming zone and social networking site (Websites for Sale)
- Alpha Master Reseller $ (Web Hosting Deals)
- Alpha Beta Pruning Search Algorithm (Computer Science)
- restrict field to alpha only (MS SQL)
- How to embed the button that i created using expression blend into the application th (C#)
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi



