ATL CImage::SetPixel not working for monochrome BMPs (nBPP=1)

163 Views Asked by At

I'm trying to code a program that changes a BMP file and adds some modifications in particular locations. The BMPs I'm trying to modify are monochrome (1 bit per pixel) as the image size needs to be quite small. I'm using the ATL CImage class to do this.

However, I can't seem to use SetPixel to change a particular pixel for monochrome BMPs.

(I've modified this code a bit for simplicity. 'color' comes from another part of the program and only ever returns RGB(255,255,255) or RGB(0,0,0))

CImage bmp;
bmp.Create(180, 1369, 1);
for (int y = 0; y < 1369; y++)
    {
        for (int x = 0; x < 180; x++) {
            bmp.SetPixel(x, y, color);
        }
    }

This code returns a black BMP when displayed. If I modify the '1' in bmp.Create, which is the number of bits per pixel, to anything larger than 8, the code works as expected. However, that fix does not suit me as I end up with a BMP that is too large.

Is there any way of making SetPixel work here?

1

There are 1 best solutions below

0
On

It appears that when you use Create() to make a monochrome bitmap that it creates one where both colors are black. You'll need to adjust the color table:

RGBQUAD colors[2] = { 0 };
bmp.GetColorTable(0, 2, colors);
colors[1].rgbRed = colors[1].rgbGreen = colors[1].rgbBlue = 0xff;
bmp.SetColorTable(0, 2, colors);

Then if you SetPixel to RGB(0xff,0xff,0xff) it should work properly