views:

155

answers:

2

Hi, could somebody explain to me why in 24-bit rgb bitmap file I have to add a padding which size depends on width of image ? What for ?

I mean I must add this code to my program (in C):

 if( read % 4 != 0 ) {
   read = 4 - (read%4);
   printf( "Padding: %d bytes\n", read );
   fread( pixel, read, 1, inFile );
  }
+1  A: 

Because 24 bits is an odd number of bytes (3) and for a variety of reasons all the image rows are required to start at an address which is a multiple of 4 bytes.

Paul R
+1  A: 

It depends on the format whether or not there is padding at the end of each row.

There really isn't much reason for it for 3 x 8 bit channel images since I/O is byte oriented anyway. For images with pixels packed into less than a byte (1 bit / pixel for example), padding is useful so that each row starts at a byte offset.

ergosys