Search code examples
cbitmapfill

Filling all bitmap 1bpp with black


I try to fill all of my bitmap 1bpp with black, but there are still some pixels left out.

I wrote that code in C:

void fillBlack(void* img, int width, int height)
{

int i=0,j=0;

for(i=0;i<width;i++)
{
    for(j=0;j<(height);j+=8)
    {
        *(char*)(img)=(*((char*)(img))^0xff);
        img++;
    }

}

}

where img is the pointer to the offset and all of the arguments are fine, especially width and height.

What am I doing wrong?


Solution

  • A bitmap image consists of scanlines with pixels. A scanline is rounded up to the nearest word. Assuming 32 bits per word:

    fillBlack(void *img, int width, int height)
    {
      char *bmp = img;
      int i,j, scanlinebytes;
    
      scanlinebytes= ((width*1)+31) / 32 * 4;    // 1 = bits per pixel
    
      for (i = 0; i < height; i++) {
        for(j = 0; j < scanlinebytes; j++) {
          *bmp++ = 0;
        }
      }
    }