Hopefully this makes sense... A 32-bit integer is 32 binary digits that represent a number (from 0 to 4 billion). An 8-bit integer is 8 binary digits that represent a number (from 0 to 255). If you want to store four things that are in that latter range, the obvious choice would be to use four separate 8-bit integers. Conceptually that works fine. However, for various reasons (performance, primarily), it's not a good way to store the four 8-bit values that comprise an image pixel. Since 4 x 8 = 32, you can smash the four 8-bit integer together and store them as a single 32-bit integer, which has the effect of solving the performance issues. Here's blue:
11111111 alpha, 00000000 red, 00011000 green, 11111111 blue 1111111000000000000000011111111 aRGB value The downside is that you can't operate directly on one of the four components without potentially effecting the other three. To operate on the four parts individually, you have to split them, which is basically dividing the 32-bit number back into four distinct 8-bit sections. You use bitwise operators to do that. There are bitwise operations to do that, in particular AND and OR operations which work just like their boolean counterparts. So if I wanted to isolate the blue value out of my strings, I could do a boolean AND with the overall value: 1111111000000000001100011111111 aRGB value 00000000000000000000000011111111 bit mask -------------------------------- 00000000000000000000000011111111 result This works just like addition, except without carrying. Each column of the answer is the result of ANDing the two corresponding bits from the value and the mask together. We're left with the blue component. However, if we do that for the green value, we'll be left with the wrong answer. 1111111000000000001100011111111 aRGB value 00000000000000001111111100000000 bit mask -------------------------------- 00000000000000000001100000000000 result The problem here is that we got the right bits left over, but the blue bits are increasing the magnitude by a factor of 2^8. You can see the obvious solution (just divide by 2^8), but there's a better way. You can use a bitwise shift operation, which just scoots the bits to the right or left a certain amount. If we scoot everything to the left by 8 bits, we'll have extracted the green component. You can do this process in reverse to "reassemble" the 32-bit number from 8-bit parts. Take the alpha part, shift left by 8, OR in the red, shift left by 8, OR in the green, shift left by 8, OR in the blue. 00000000000000000000000000000000 empty start 00000000000000000000000011111111 alpha -------------------------------- (ORed) 00000000000000000000000011111111 result shift 00000000000000001111111100000000 00000000000000000000000000000000 red -------------------------------- (ORed) 00000000000000001111111100000000 result shift 00000000111111110000000000000000 00000000000000000000000000011000 green -------------------------------- (ORed) 00000000111111110000000000011000 result shift 11111111000000000001100000000000 00000000000000000000000011111111 blue -------------------------------- (ORed) 11111111000000000001100011111111 result (the original color from above) Hopefully that all makes sense. cheers, barneyb On 12/12/06, Rick Root <[EMAIL PROTECTED]> wrote: > I've been trying to do a freakin' blur all day long and i'm having some > trouble with math conversions. > > Java stores RGB values in an image as a 32 bit integer, containing the > alpha, red, green, and blue values in each 8 bits. > > In my world, the alpha is always non-transparent, ie 255... > > For example... > > 255,255,0,0 = a red pixel > 255,255,255,0 = a yellow pixel > 255,0,0,255 = a white pixel. > > The problem is I'm having trouble grasping conversion between 4 sets of > 8 bit numbers and a 32 bit number. > > > I know it involves bit shifting and stuff like that but .. I'm just not > getting it. > > Could someone explain it to me like I'm a 6 year old? (apologiez to > Denzel Washington for that) > > Rick -- Barney Boisvert [EMAIL PROTECTED] 360.319.6145 http://www.barneyb.com/ Got Gmail? I have 100 invites. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| Create robust enterprise, web RIAs. Upgrade & integrate Adobe Coldfusion MX7 with Flex 2 http://ad.doubleclick.net/clk;56760587;14748456;a?http://www.adobe.com/products/coldfusion/flex2/?sdid=LVNU Archive: http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:263815 Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4

