Saturday, May 27, 2017

BitBlt documentation doesn't mention monochrome to color blitting

I was trying to understand more about BitBlt in theForger's "Transparent Bitmaps" tutorial. What I didn't know was that there's a special conversion process where if you blit a monochrome bitmap to a color bitmap (aka the color bitmap is the hdcDest parameter of BitBlt and the monochrome is the hdcSrc parameter, according to the docs), the monochrome bitmap is mapped to a color bitmap first, like this:

-The white color is mapped to the color bitmap's background color (set from SetBkColor).
-The black color is mapped to the color bitmap's text color (set from SetTextColor).

After that, the 2 images are combined. This was mentioned in this msdn blog post by Raymond Chen. A comment from 'Mike Dimmick' highlights this too. What sucks is that it wasn't mentioned in the BitBlt docs today. That really confused me a lot. Well, anyways, happy to know there's no super magic, just still bitwise ops in the end.

I asked about this in the msdn forums, but I didn't mention about a 'monochrome' bitmap or using SetBkColor or SetTextColor. I didn't know that was relevant til later on. I put my own answer there. I made a diagram too about it, thought it could help anyone in the future.




(The text is hard to see, sorry, I was putting in an image. Opening it in a new tab or downloading it should help to view it easier.)

Also, I was wondering about how the bitwise operations happened in the first place too, with two pixels. Someone at SO helped me with that. Thanks. Basically, in a sense, you can visualize the process by converting the colors to 8 bit hexadecimal and do the bitwise op there. For example, with an AND operation:

RGB(66,244,134) (light green) AND RGB(66, 155, 244) (light blue)

(light green)
66 -> 0x42
244 -> 0xf4
134 -> 0x86
(0x42f486)

(light blue)
66 -> 0x42
155 -> 0x9b
244 -> 0xf4
(0x429bf4)


0x42f486  AND 0x429bf4
0b10000101111010010000110 AND 0b10000101001101111110100
0b10000101001000010000100

0x429084
0x42 -> 66
0x90 -> 144
0x84 -> 132

RESULT -> 0x429084, RGB(66,144,132) (some greenish blue or something)


No comments:

Post a Comment