0

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?

  • Maybe `color` needs to be `0` or `1`? – Mark Ransom Dec 19 '20 at 20:58
  • I had already tried that(probably should've mentioned it!). I changed 'color' to 1, to see if it would at least output a white BMP. It still outputs black. I also tried 0 just in case 1 was black and 0 was white, but nope. – GoodCrossing Dec 19 '20 at 21:02
  • If you create a white monochrome bitmap and open it and do a GetPixel(0,0) what value do you get? – ericpat Dec 23 '20 at 13:32

1 Answers1

0

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

ericpat
  • 94
  • 5