0

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.

1 Answer 1

2

In the first case, you want to wind back for all but the last iteration.

In the second case, you would need a different condition, as you want to revert to the beginning of the line in all but the first iteration (where you already are in the right position). So if (m > 0) should work.

1
  • I tried if (m != 0) before but it did not work. Maybe there was something wrong with something else. Now both solutions work. Thanks so much, Blauelf.
    – iEarvin
    Aug 2, 2019 at 12:12

You must log in to answer this question.

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