« excellent nytimes photo/info piece | Main | weather »
Friday
Dec312004

most obfuscated code I've ever seen

I'm porting something from java to C#. I'm pretty sure what I'm trying to figure out at the moment is the Bresenham line drawing algorithm, which tells you how to draw a line on a bitmap. But then I get to this line, where it actually sets the pixel value in the buffer:



int rand8 = Shade3D.nextRandom8Bit();

pbuf[offset] = (n > nMid?(rand8 < 85?argb1Dn:(rand8 > 170?argb1Up:argb1)):(rand8 < 85?argb2Dn:(rand8 > 170?argb2Up:argb2)));



So first it gets a random 8 bit integer, if the method name can be trusted, and then

it decides what value to put in the pixel by whether the random is less than 85, less than 170, or greater than 170.



Okay, wait -- I might actually understand this. This might actually be antialiasing. Or it might be utter witchcraft.



This section also includes code that shifts bits left and right, and a lot of code that pays a lot of attention to 8 bit vs 16 bit vs 32 bit integers. As a rule, something is harder to port the closer it is to hardware. Bit-shifts and paying attention to the bit-width of types is rather close to the hardware, for my taste.



So have I just spent two day's on a fool's errand? Perhaps. I'm probably going to have to throw away the last two days of work, and re-start the port with another approach. Damn it. At least I get to keep the lessons learned.



And yes, it is new year's eve and I'm programming. It's not exactly fun, but it's what makes my life better.

Reader Comments (1)

int rand8 = Shade3D.nextRandom8Bit();
pbuf[offset] = (n > nMid?(rand8 < 85?argb1Dn:(rand8 > 170?argb1Up:argb1)):(rand8 < 85?argb2Dn:(rand8 > 170?argb2Up:argb2)));

Wow... That kind of code actually exists in this world? o.O Hmm, I wonder if there's a generic way to "decipher" such code. hehe

12.31.2004 at 05:57 PM | Unregistered CommenterBrian Lee

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>