Last time we set up a very simple project that will allow us to display and manipulate image data using a WriteableBitmap. Before we can do anything cool though, we need to understand a few things about the WriteableBitmap class.
Pixel Collection
The WriteableBitmap has an integer array called Pixels that represents the 2D texture of the bitmap. In most cases we will be looping through Pixels (using PixelWidth and PixelHeight) to perform our operations. You can find the data for a pixel located at (x,y) in the WritableBitmap wb with:
int color = wb.Pixels[x + y * wb.PixelWidth];
Without knowing how the color is stored we really can’t do much with this data. Let’s take a peak under the hood.
Pixel Format
The format used by the Silverlight WriteableBitmap is ARGB32 (premultiplied RGB – we’ll cover this later). This means that the color is represented by a 32-bit integer with the following format:
As you can see, each channel (alpha, red, green, and blue) uses 8 bits, giving a possible 28 or 256 intensities for each (0-255).
Extracting Color Components
Extracting each component intensity can be done by masking the color value with 0xFF then shifting the number right by 8 bits. The heck you say?
Bitwise Operators
And
As their name implies, bitwise operators operate on one bit at a time. The operator we are concerned with for now is & (AND).
operator | bit one | bit two | output |
& | 0 | 0 | 0 |
& | 0 | 1 | 0 |
& | 1 | 0 | 0 |
& | 1 | 1 | 1 |
Notice in the & operator when a bit is 0 the output bit will always be 0. Wherever there is a 1 in a bit, the result is always the value of the other bit. This trait will allow us to create what is called a mask (or bitmask). We cover the bits we don’t care about with 0. So the mask 0xFF above in binary is…
& this with the pixel and we will get the value for the last 8 bits of the color.
Let’s work with an example. I randomly picked #FFDB91D6. #FFDB91D6 in binary:
&
=
Voila.
Right Shift
If we were to use the & alone we could only recover the values for the blue channel. If only we could shift the color bits to match our mask… Turns out there is another handy bitwise operator for doing just that: >> (RIGHT SHIFT)
operator | input | shift | output |
>> | 10001000 | 1 | 01000100 |
>> | 10001000 | 2 | 00100010 |
>> | 10001000 | 3 | 00010001 |
>> | 10001000 | 4 | 00001000 |
Notice how the bits shift right? This is equivalent to dividing by powers of 2. Don’t let the leading zeros confuse you. If you divide 1120 by 10 (in base 10) you get 0112. If you divide again you get 0011 (drop the remainder). See?
>> 8
=
Now the green bits match our mask. Hooray!
Full Example
Let’s run through our example from above (#FFDB91D6) in its entirety:
1.) First, let’s get the blue bits:
&
2.) Shift the bits right 8 places:
>> 8
=
3.) Recover the green bits:
&
=
4.) Shift the bits right another 8 places:
>> 8
=
5.) Extract the red bits:
&
6.) Last shift!!!
>> 8
=
7.) Extract the alpha bits:
We don’t have to mask the last value because it’s already exactly what we need. Let’s see how this plays out in code.
int pixel = (int)0xFFDB91D6;
byte blue = (byte)(pixel & 0xFF);
pixel >>= 8;
byte green = (byte)(pixel & 0xFF);
pixel >>= 8;
byte red = (byte)(pixel & 0xFF);
pixel >>= 8;
byte alpha = (byte)(pixel);
Quite elegant and very efficient.
Summary
Recovering colors from the WriteableBitmap is pretty straightforward once you understand the pixel structure and how to mask them.
Download Code
Note: This code doesn’t actually modify the image in any way. We will do that in the next installment.
Up next: Encoding Color