Padding a Bitmap pixel array

后端 未结 1 430
傲寒
傲寒 2020-12-21 20:35

I\'m making a program that creates a bitmap file in C. it\'s using 24-bit colour.

I\'m writing the file in 3 stages, i first write the FileHeader, then the InfoHeade

相关标签:
1条回答
  • 2020-12-21 21:01

    You are not padding rows to word size, you are padding the current file position. And it doesn't work because the size of your headers add up to 54 -- not a multiple of 4.

    Instead of using ftell to retrieve the 'current position', use maths. Make your pad an unsigned long, and insert before your loops:

    int npad = (sizeof(IMAGE)*width) & 3;
    if (npad)
      npad = 4-npad;
    

    Then, instead of the while(ftell .. loop, write out the number of required bytes immediately:

    fwrite (&pad, 1,npad, n_img);
    

    npad will range from 0..3, that's why you have to make pad a 4-byte integer.

    0 讨论(0)
提交回复
热议问题