Pset 4 resize - problems with resizing

Can anyone help point out what's wrong with my resize algorithm? I have researched and tried out many ways to do it and watched the walkthrough so many times. Here's the resizing part of my code and note that bi.biWidth is the resized width.

`

// temporary storage
RGBTRIPLE triple;
RGBTRIPLE *scanline = malloc(bi.biWidth * sizeof(RGBTRIPLE));

// iterate over infile's scanlines
for (int i = 0; i < abs(oldheight); i++)
{
//iterate over pixel line by line
for (int pixel = 0; pixel < oldwidth; pixel++)
{
// read RGB triple from infile

// store in array n times to resize horizontally
for (int c = 0; c < n; c++)
{
scanline[pixel + n] = triple;
}
}
// skip over padding, if any

//resize vertically
for (int v = 0; v < n; v++)
{
// iterate over pixels in array and write it n times
for (int j = 0; j < sizeof(scanline); j++)
{
fwrite(&scanline[j], sizeof(RGBTRIPLE), 1, outptr);
for (int k = 0; k < newpadding; k++)
{
fputc(0x00, outptr);
}
}
}
}

`

This line has a couple of problems:

scanline[pixel + n] = triple;

Let's say that the scale factor, n, is 3. Now, let's walk through what happens. The code starts by reading the first pixel in a line and stores it in triple. Next, the for loop to write to the array executes. In the first pass, c=0, and the line above writes the first pixel, triple, to scanline[0+3], or scanline. The loop runs 2 more times and rewrites the same pixel to the same position. That's the first problem.

The second problem then shows up. The code loops back around and reads the second pixel, and then tries to write that pixel to scanline[1+3], or scanline a total of 3 times.

Don't understand the second problem yet? Let's remove the first problem to highlight it. Say that the line above were this instead:

scanline[pixel + c] = triple;

It would write the first pixel to scanline, scanline, and scanline. Fine so far, but then it loops around and writes the next pixel to scanline[1+0], or scanline, followed by scanline and scanline. It should have written to positions 3, 4, and 5 instead of overwriting 1, 2, and 3.

These are the major problems. There may be more, but this should get you going.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

• Thank you, this helped a lot although there were many other problems with my code which I found later. Dec 27 '18 at 3:26