0

I've been stuck on resize for a while and I think I nearly have it. My outputs are nearly accurate except for the fact that some of the pixels are not being placed properly, and I think this has something to do with the padding.

Here is the original image Here is the original image

and the resized image and the resized image

Looking into their hexadecimals it seems to have something to do with the fact that 0x00's are being padded into the wrong places, but I'm not sure why this is the case.

    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    // iterate over pixels in scanline
    for (int j = 0; j < old_width; j++)
    {
        // temporary storage
        RGBTRIPLE triple;

        // read RGB triple from infile
        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

        // write RGB triple to outfile
        for (int k = 0; k < n; k++) //Does everything n times
        {
           fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
        }

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

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

    //move cursor back to recopy for height
    if (i < n - 1)
    {
        fseek(inptr, -old_width * sizeof(RGBTRIPLE) - old_padding, SEEK_CUR);
    }

}

1 Answer 1

0

if (i < n - 1) will trigger for the first input line only, second input row starts with i being of value n. A possible fix would be if (i % n < n - 1), you might know the operator from pset2 Caesar and Vigenère.

3
  • Thank you, that fixed the problem but I'm actually very confused as to why it did. Could you explain it in a bit more detail please? Specifically why did (i < n - 1) only trigger once?
    – user21490
    Commented Apr 26, 2018 at 7:34
  • Ahhhh, i will keep increment beyond that function, so it'll only works the first time.
    – user21490
    Commented Apr 26, 2018 at 7:42
  • An alternative would have been to divide your outer loop into two nested loops, one over the input file, the other repeating n times. Also, there are solutions without the re-read (storing a line in an array), which I prefer, but yours is probably as valid here.
    – Blauelf
    Commented Apr 26, 2018 at 8:57

You must log in to answer this question.