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.