Through the windows API I can get a void* to the bit data of a bitmap that I’ve just created. (Parameter ppvBits in Function:
HBITMAP CreateDIBSection(
__in HDC hdc,
__in const BITMAPINFO *pbmi,
__in UINT iUsage,
__out VOID **ppvBits,
__in HANDLE hSection,
__in DWORD dwOffset
); )
I declare on creation that the bitmap is to be 8-bits per pixel. I therefore cast from the void* to an unsigned char* and access the data through dereference.
Is this safe? Is it always guaranteed that each 8-bit pixel will be stored in one byte?
Is this safe? Is it always guaranteed that each 8-bit pixel will be stored in one byte?
Yes. That is exactly what you're supposed to do.
Keep in mind that bitmaps have a "pitch" which isn't necessarily equal to their length. The pitch is the number of bytes between rows in the bitmap. The pitch is padded to 4 byte boundaries, so if you have a width of 198 pixels, you will actually have a pitch of 200 bytes.
Also keep in mind that the bitmap is stored "bottom up" which is backwards from what you might expect. You can make the bitmap be a normal "top down" bitmap by specifying a negative height.
OK, thanks.
I was aware of of what you mentioned for bitmaps, although you've reminded me to check for the pitch :). I'm using it to store 1 bit data for device calibration and my tests pass but the device has a row length of 600, I'll have to add in for pitch to keep my code generic. Thanks again.
At first I thought to use a bool but then realised that C++ allocates an entire byte for bool storage for efficiency reasons. I then thought, following from this, that maybe certain implementations may use more than a byte (ie two bytes). But if this was the case then the unsigned char would probably be two bytes so everything would be OK anyway.