Search code examples
c++windowswinapibitmapbmp

C++ Bitmap Bit per pixel


I'm trying to understand building a bmp based on raw data in c++ and I have a few questions.

My bmp can be black and white so I figured that the in the bit per pixel field I should go with 1. However in a lot of guides I see the padding field adds the number of bits to keep 32 bit alignment, meaning my bmp will be the same file size as a 24 bit per pixel bmp.

Is this understanding correct or in some way is the 1 bit per pixel smaller than 24, 32 etc?

Thanks


Solution

  • Monochrome bitmaps are aligned too, but they will not take as much space as 24/32-bpp ones.

    • A row of 5-pixel wide 24-bit bitmap will take 16 bytes: 5*3=15 for pixels, and 1 byte of padding.
    • A row of 5-pixel wide 32-bit bitmap will take 20 bytes: 5*4=20 for pixels, no need for padding.
    • A row of 5-pixel wide monochrome bitmap will take 4 bytes: 1 byte for pixels (it is not possible to use less than a byte, so whole byte is taken but 3 of its 8 bits are not used), and 3 bytes of padding.

    So, monochrome bitmap will of course be smaller than 24-bit one.