Search code examples
cimage-resizingbmp

what does this mean {fseek (inptr, - ( bi.biWidth * 3 + padding), SEEK_CUR);} in the program that resizes a bmp image?


I don't quite understand the necessity of this code here.

// skip over padding, if any
fseek(inptr, padding, SEEK_CUR);

// then add it back (to demonstrate how)
for (int k = 0; k < new_padding; k++)
{
    fputc(0x00, outptr);
}

if (repeat < n - 1)
{
    fseek(inptr, - (bi.biWidth * 3 + padding), SEEK_CUR);
}

Solution

  • It is difficult to confirm the correctness or even the pertinence of this code fragment without the full context.

    fseek moves the input stream's pointer forward by padding bytes.

    The program then outputs new_padding null bytes to the output stream, presumably a different amount from padding. Microsoft's BMP file format requires some padding in various places, for easier reading into memory.

    Finally, fseek is used again to set the input stream pointer backward to the beginning of the row of pixels (3 bytes per pixel) plus the padding it skipped, but only if (repeat < n - 1).