I'm struggling with when to put the pointer back to the beginning.
Every time I want to copy the same scanline vertically, I know I need to move the pointer on the input file from the end of the current scanline to the beginning by:
// copy the same scanline 'enlargeBy' times
for (int m = 0; m < enlargeBy; m++) {
// iterate over pixels in scanline
for (int j = 0; j < originalWidth; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// write RGB triple to outfile 'enlargeBy' times
for (int l = 0; l < enlargeBy; l++) {
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
// add paddings to outfile
for (int k = 0; k < enlargedPadding; k++)
fputc(0x00, outptr);
// move the pointer back to the beginning of the current scanline
if (m < enlargeBy - 1)
// fseek moves the pointer by bytes
fseek(inptr, -originalWidth * 3, SEEK_CUR);
}
This works which moves the pointer AFTER writing the needed RGBs and paddings for every scanline.
My question is: Why moving the pointer BEFORE writing the needed RGBs and paddings for every scanline doesn't work?
The code is like this:
// copy the same scanline 'enlargeBy' times
for (int m = 0; m < enlargeBy; m++) {
// move the pointer back to the beginning of the current scanline
if (m > enlargeBy - 1)
// fseek moves the pointer by bytes
fseek(inptr, -originalWidth * 3, SEEK_CUR);
// iterate over pixels in scanline
for (int j = 0; j < originalWidth; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// write RGB triple to outfile 'enlargeBy' times
for (int l = 0; l < enlargeBy; l++) {
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
// add paddings to outfile
for (int k = 0; k < enlargedPadding; k++)
fputc(0x00, outptr);
}
Thanks so much for helping.