Can you clearly explain what exactly is padding in pset4? Why I need to add it?


as explained per the specs, the size of each scanline in a BMP has to be multiple of 4. padding bytes are appended to each scanline in case its size wasn't multiple of 4 in order to achieve the rule above (i.e., make the size multiple of 4).

for example, in a 3 × 3 BMP, the size of each scanline is 3 pixels × sizeof(RGBTRIPLE) which is equal to 9 bytes (because sizeof(RGBTRIPLE) is evaluated to 3).

9 is certainly not a multiple of 4, so we add extra 3 bytes (usually, but no necessarily, 0s) at the end of each scanline to get the size to the nearest greater multiple of 4 (in this case 12).

as for the reason for using padding, the short answer, as illustrated by the Wikipedia article, the people who designed the BMP format decided that each scanline has to begin at a memory address that is multiple of 4 for optimization purposes when loading a BMP image into memory.

  • then why padding is stored in variable type int,when it is expressed in bytes? Jul 1 '16 at 9:27
  • @Himanshu Jhamb, we don't store the padding bytes themselves. we read them from and write them to BMPs. we store the number of padding bytes to be able to read/write the correct number of them. what type of variable would you suggest to store that number?
    – kzidane
    Jul 1 '16 at 9:35
  • what if we use BYTE for padding? Jul 2 '16 at 14:40
  • well, since the padding is the remainder of dividing some expression by 4, it's always gonna be in [0, 3] and that's of course is in the range of BYTE, so that should work too.
    – kzidane
    Jul 2 '16 at 15:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .