## January 30, 2006

### Pixels and Bit Twiddling, pt 1

I was discussing bit manipulation with somebody and it reminded me of one of my single favorite pixel munging routines. I've purposely omitted the NAME of the function - but its pretty cool ("One from the book", as Paul Erdos might say). And this one was really originally written (meaning he showed me the trick first) by the prime "PixelBoy" himself, Ben Weiss.

inline uint32 FUNCTION_NAME_OMITTED (uint32 a, uint32 b, uint32 mask=0x01010100)
{
uint32 s = (a + b);
uint32 c = (a ^ b ^ s) & mask;
return (s-c) (c-(c>>8));
}

Any guesses as to what it does?

I'll give you a hint: its a 32 bit pixel operation, but doesn't preserve alpha in this case (if only you could access the carry bit from plain old C ... ).... Just decoding what this did taught me a LOT about graphics coding, back in the day.

Dossy Shiobara said...

Combining the value of two pixels, then doing an XOR of the two pixels then XORed by their sum and clamped at 0xFF for each RGB value using the bitwise and of the 24-bit 0x01010100 mask to pick up the carry bit.

Looks like some pixel blending algorithm that lightens one pixel by the value of another? Probably useful in 3D shading, or maybe old-school 2D sprite animation.

Boy, it's been years since I've done any pixel-manipulation work. Brings back memories ...

Sree Kotay said...

Hey, not bad - actually, you pretty much nailed it (but then overthought it :P).

Its a saturated add - it'll simply add two rgb values, but clamp the results of each "channel" (i.e. each byte, for the lower 3 bytes - as I mentioned, doesn't QUITE work for the alpha channel) to 255 - with no compares required...

Anonymous said...

Anonymous said...
Anonymous said...

[url=http://buy-aoc-gold.rgtrcredit.com/][color=#800080][b]Age Of Conan Gold [/b][/color][/url][url=http://www.aocsale.com/][color=#800080][b]Age Of Conan Gold [/b][/color][/url][url=http://www.buy-cheap-aoc-gold.com/][color=#800080][b]Age Of Conan Gold[/b][/color][/url][url=http://www.buy-cheap-aoc-gold.com/][color=#800080][b]buy age of conan gold [/b][/color][/url]

http://www.aocsale.com

Age Of Conan Gold     Age Of Conan Gold       Age Of Conan Gold     buy age of conan gold
buy age of conan gold   buy age of conan gold          aoc gold             aoc gold             aoc gold

Anonymous said...

wow goldwow goldaoc goldwow gold cheap wow gold are all in gamelee.

Anonymous said...
Anonymous said...
Anonymous said...

<a href=" http://seo.cuteseo.cn/" >搜索引擎优化</a>
<a href=" http://kangjie.cn/NewsInfo.asp?id=146" >木门</a>
<a href=" http://kangjie.cn/NewsInfo.asp?id=147" >推拉门</a>
<a href=" http://kangjie.cn/NewsInfo.asp?id=147" >推拉门</a>
<a href=" http://www.tongchuang2008.com/" >翻译公司</a>
<a href=" http://www.tongchuang2008.com/" >北京翻译公司</a>
<a href=" http://www.tongchuang2008.com/" >英语翻译</a>
<a href=" http://www.hdssw.com" >花店</a>
<a href=" http://www.cnnb315.com" >装修大学</a>

<a href="http://www.htsky168.com/" >短信猫</a>
<a href="http://www.bjksjy.cn/" >北京儿童医院</a>
<a href="http://www.bjksjy.cn/" >北京协和医院</a>
<a href="http://www.bjksjy.cn/" >北京同仁医院</a>
<a href="http://www.bjksjy.cn/mymy_menzhen.asp?id=184" >北京儿童医院</a>
<a href="http://www.bjksjy.cn/mymy_menzhen.asp?id=185" >北京协和医院</a>
<a href="http://www.bjksjy.cn/mymy_menzhen.asp?id=178" >北京同仁医院</a>

<a href="http://www.bjksjy.cn/mymy_menzhen.asp?id=196" >北京医院</a>
<a href="http://www.bjksjy.cn/mymy_menzhen.asp?id=194" >北京大学口腔医院</a>
<a href="http://www.bjksjy.cn/mymy_menzhen.asp?id=193" > 北京天坛医院 </a>
<a href="http://www.bjksjy.cn/mymy_menzhen.asp?id=192" >北京空军总医院</a>
<a href="http://www.bjksjy.cn/mymy_menzhen.asp?id=191" >北京广安门医院</a>
<a href="http://www.bjksjy.cn/mymy_menzhen.asp?id=190" >中国人民解放军总医院</a>
<a href="http://www.bjksjy.cn/mymy_menzhen.asp?id=189" >北京大学第三医院 </a>
<a href="http://www.bjksjy.cn/mymy_menzhen.asp?id=188" >北京阜外心血管病医院</a>
<a href="http://www.bjksjy.cn/mymy_menzhen.asp?id=187" >北京中医医院</a>
<a href="http://www.bjksjy.cn/mymy_menzhen.asp?id=186" >北京肿瘤医院</a>
<a href="http://www.bjksjy.cn/mymy_menzhen.asp?id=175" >北京积水潭医院</a>
<a href="http://www.bjksjy.cn/mymy_menzhen.asp?id=174" >北京安贞医院</a>
<a href="http://www.bjksjy.cn/mymy_menzhen.asp?id=173" >北京宣武医院 </a>

<a href="http://www.bqgl.cn " >北京租车</a>
<a href="http://www.bqgl.cn " >租车</a>
<a href="http://www.bqgl.cn " >汽车租凭</a>
<a href="http://www.bqgl.cn/index_gsjj.asp" >北京北汽国际旅行社</a>
<a href="http://www.bqgl.cn/index_xwzx.asp" >北京北汽国际旅行社</a>
<a href="http://www.bqgl.cn/index_cxzs.asp" >北京北汽国际旅行社</a>
<a href="http://www.bqgl.cn/index_sxlc.asp" >北京北汽国际旅行社</a>
<a href="http://www.bqgl.cn/index_zcbj.asp" >北京北汽国际旅行社</a>
<a href="http://www.bqgl.cn/index_fwkh.asp" >北京北汽国际旅行社</a>
<a href="http://www.bqgl.cn/liuyan/" >北京北汽国际旅行社</a>
<a href="http://www.bqgl.cn/index_lxwm.asp" >北京北汽国际旅行社</a>
<a href="http://www.bqgl.cn/index_lypd.asp" >北京北汽国际旅行社</a>

<a href="http://www.2dd88.cn " >江西二手车</a>
<a href="http://www.2dd88.cn " >南昌二手车</a>
<a href="http://www.2dd88.cn/sell_car_search.asp?provice_shi=%C4%CF%B2%FD%CA%D0" >南昌市</a>
<a href="http://www.2dd88.cn/sell_car_search.asp?provice_shi=%BE%B0%B5%C2%D5%F2%CA%D0" >景德镇市</a>
<a href="http://www.2dd88.cn/sell_car_search.asp?provice_shi=%C6%BC%CF%E7%CA%D0" >萍乡市</a>
<a href="http://www.2dd88.cn/sell_car_search.asp?provice_shi=%D0%C2%D3%E0%CA%D0" >新余市</a>
<a href="http://www.2dd88.cn/sell_car_search.asp?provice_shi=%C9%CF%C8%C4%CA%D0" >上饶市</a>
<a href="http://www.2dd88.cn/sell_car_search.asp?provice_shi=%D3%A5%CC%B6%CA%D0" >鹰潭市</a>
<a href="http://www.2dd88.cn/sell_car_search.asp?provice_shi=%BC%AA%B0%B2%CA%D0" >吉安市</a>
<a href="http://www.2dd88.cn/sell_car_search.asp?provice_shi=%B8%D3%D6%DD%CA%D0" >赣州市</a>
<a href="http://www.2dd88.cn/sell_car_search.asp?provice_shi=%B8%A7%D6%DD%CA%D0" >抚州市</a>
<a href="http://www.2dd88.cn/sell_car_search.asp?provice_shi=%D2%CB%B4%BA%CA%D0" >宜春市</a>