1

Can I request for some help on my resize.c pset? I'm trying to do the "rewrite" method as per the pset's walkthrough. The pseudocode is as follows:

1 for each row
2   for each pixel
3        write to array n times
4   for n times
5       write array to outfile
6       write outfile padding
7 skip over infile padding

Below is what I have so far. I added some comments to map each line to its corresponding pseudocode above. I can't seem to find where my code went wrong

// iterate over infile's scanlines (line 1)
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    // store pixels here.
    RGBTRIPLE pixels[n];

    // iterate over pixels in scanline (line 2)
    for (int j = 0; j < bi.biWidth; j++)
    {
        // temporary storage
        RGBTRIPLE triple;

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

        // write pixel to array n times (line 3)
        for (int k = 0; k < n; k++)
        {
            pixels[k] = triple;
        }

    }

    // (line 4)
    for (int l = 0; l < n; l++)
    {
        // write array to outfile. (line 5)
        for (int m = 0; m < n; m++)
        {
            fwrite(&pixels[m], sizeof(RGBTRIPLE), 1, outptr);
        }

        // write outfile padding (line 6)
        for (int o = 0; o < out_padding; o++)
        {
            fputc(0x00, outptr);
        }
    }

    // skip over infile padding. (line 7)
    fseek(inptr, in_padding, SEEK_CUR);
}

Here's a gist of the whole thing for reference. Any hints and tips will be greatly appreciated!

2
  • Where are you initializing n? Sep 12 '17 at 18:14
  • @toyotasupra n is the factor to be resized with, passed as a command line argument.
    – clearh
    Sep 12 '17 at 20:54
0

Solved it! What I'm doing wrong is that I keep on overwriting the pixels stored in the pixels array in this block

// write pixel to array n times (line 3)
for (int k = 0; k < n; k++)
{
    pixels[k] = triple;
}

The solution is to create a separate iterator that increments for each iteration of the loop. And also, the pixels array is initalized with a wrong length (n pixels is not enough!)

You must log in to answer this question.

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