Dealing with padding in a BMP file in C

后端 未结 3 1416
一生所求
一生所求 2020-12-20 00:45

I am trying to edit a BMP file in C. My code works for BMP files with no padding but I am having trouble dealing with padding.

There are a few other questions on BMP

3条回答
  •  一向
    一向 (楼主)
    2020-12-20 01:21

    The "padding bytes" are the bytes following the pixels of a scanline. You are not so interested in the padding as in the scanline size and pixel size:

    iScanlineSize  = ((width * bitsperpixel) + 31) / 32 * 4;
    iBytesperPixel = bitsperpixel / 8;
    

    Now you can loop over scanlines and adress pixels and pixel parts (colors) as follows:

    for (int a = 0; a < height; a++) {
        for (int b = 0; b < width; b++) {
            for (int c = 0; c < iBytesperPixel; c++) {
                pixelPart= bmpArray[a*iScanlineSize + b*iBytesperPixel + c];
            }
        ]
    }
    

提交回复
热议问题