Creating 8bpp bitmap with GDI and saving it as a file

后端 未结 3 1045
礼貌的吻别
礼貌的吻别 2021-01-15 06:46

I have a perfectly working code that creates 32bpp bitmap and I need to change it so that 8bpp bitmap is created.

Here\'s the piece of code that creates 32bpp bitmap,

相关标签:
3条回答
  • 2021-01-15 07:03

    You need to generate a palette for your image. Each pixel in a 32bit image is stored as 8-bits for Alpha, Red, Green and Blue. Where as in a 8bit image, the value in each pixel an 8bit index into the palette.

    Your for(i=0..255) { bmi.bmiColors[i].rgbRed = i; ....} code is generated an grey-scale palette.

    If the whole image is coming out as grey then it sounds like an alignment error, from memory the width of a palettized image must be a multiple of 4.

    Try saving a SMALL 256 colour (aka 8-bit image) from Paint and compare in a hex editor.

    0 讨论(0)
  • 2021-01-15 07:04

    It looks like an alignment problem. Make sure you update bfOffBits in the BITMAPFILEHEADER so that it points to the first byte of the pixel data. (If you don't change it, then it probably points to the beginning of the palette.)

    In other words, sizeof(RGBQUAD)*256 should be added here as well:

    bf.bfOffBits = sizeof(BITMAPFILEHEADER) + bmi.bmiHeader.biSize;
    

    Also makes sure the first scanline starts on a DWORD boundary. That is, its offset from the beginning of the file should be a multiple of four bytes. Likewise, each scanline should be padded out to a multiple of four bytes. (You may not see these problems if your widths are nice even numbers. It's good to have an odd-width image among your test cases.)

    0 讨论(0)
  • 2021-01-15 07:09

    You need to specify the size of the palette that you attached. Right now it's zero, so the palette is showing up as the first bunch of pixels in your image.

    bmi.bmiHeader.biClrUsed = 256;
    
    0 讨论(0)
提交回复
热议问题