1

I have been stuck on resize for a while. I am currently struggling on the vertical portion of resize. I am using the "rewrite" method as explained in the problem spec. My program does resize an image correctly if the value is one, but any value greater and the program exits with a runtime error - a value overstepped the bounds of the array. Can someone lead me to a way to fix this?

Edited out code as per CS50 Regulations

1 Answer 1

4

After edit:

For reference, here is the pseudocode from the walkthrough:

for each row
      for each pixel
         write to array n times
      for n times
         write array to outfile
         write outfile padding
      skip over infile padding

As I understand it, array is going to hold one new row of RGBTRIPLEs. The declaration would be RGBTRIPLE array[newbi.biWidth]. You do not need sizeof(RGBTRIPLE) because it is already a collection of RGBTRIPLE objects.

This "new row" will be built horizontally ("write to array n times", which is the j loop), and then written vertically ("for n times...", which is the l loop). You always want to start building it at array[0] and writing it from array[0]. Therefore you need to reset counter3 to 0 before you start the next scanline.

There are some problems in the l loop. To match the pseudo code it would be more like this:

for (int l = 0; l < n; l++)
{
    fwrite......
    write padding
}
fseek....

The fwrite only needs to write array to the outfile. You shouldn't iterate over the pixels, you already did that in the j loop. And you need to write the whole array.
fwrite(&array, sizeof(array),1,outfile) is more like it.


Before edit: This fwrite(array, sizeof(RGBTRIPLE), newbi.biWidth, outptr); always writes the calculated number of bytes from the first byte in array. Don't you need to reset counter3 = 0; somewhere?

5
  • Please check out my revised question and code. Thanks in advance.
    – Jason_V
    Commented Apr 21, 2017 at 4:16
  • How is array declared? Commented Apr 21, 2017 at 12:06
  • @DinoCoderDaaurus What's wrong with its declaration? It just a standard TYPE NAME[SIZE}; array declaration. Is there a specific reason I need to use malloc?
    – Jason_V
    Commented Apr 21, 2017 at 12:10
  • Oops, sorry, I didn't see it, now I do. Commented Apr 21, 2017 at 13:01
  • Many thanks my friend! It appears I was heading in the wrong direction before your help.
    – Jason_V
    Commented Apr 22, 2017 at 5:00

You must log in to answer this question.

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