I was thinking about the way Casey did is unmasking of the bitmap file and i started thinking if that was the easiest way of doing it. So i started play with some bitoperations to see if I could come up with some other way of doing it without looping trough all bits. Mostly because i wanted more experience in what bit operators do.
Anyway, this is what i come up with, I’m not saying it’s a better way, nor that its better looking. I just wanted to do it in another way without the loop count thing.
I also didn’t take account for the alpha channel but it wouldn’t be any problem implementing it i guess.
Maybe this is just a stupid way of doing it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | Int32 someColor = 0x123456;
Int32 maskRed = 0XFF0000;
Int32 maskGreen = 0X00FF00;
Int32 maskBlue = 0X0000FF;
Int32 redOut = maskRed & someColor;
Int32 greenOut = maskGreen & someColor;
Int32 blueOut = maskBlue & someColor;
if (redOut >> 16 == 0) redOut = redOut << 8;
if (redOut >> 16 == 0) redOut = redOut << 8;
if (greenOut >> 16 != 0) greenOut = greenOut >> 8;
if (greenOut >> 8 == 0) greenOut = greenOut << 8;
if (blueOut >> 16 != 0) blueOut = blueOut >> 16;
if (blueOut >> 8 != 0) blueOut = blueOut >> 8;
|
Would it perhaps be an even more efficient way of swapping the channels?