It looks like my horizontal resizing is working, but the vertical isn't. I can't figure out where I'm going wrong.

I've been testing as described in the pset.

./resize 4 small.bmp student.bmp
~cs50/pset4/resize 4 small.bmp staff.bmp
~cs50/pset4/peek student.bmp staff.bmp

Here's what I know:

  • My student.bmp headers match the staff.bmp headers.
  • My student.bmp is the same size as staff.bmp and has the same dimensions (12 x12).
  • My student.bmp file looks like this: student.bmp

Here's the code for the set of nested loops where I'm trying to resize.

/ iterate over the infile's scanlines
for (int i = 0; i < abs(biIn.biHeight); i++)

    // repeat row copying n times to resize vertically
    for (int inRow = 0; inRow < n; inRow++)

        // iterate over the pixels in each line
        for (int inCol = 0; inCol < biIn.biWidth; inCol++)
                // temporary storage
                RGBTRIPLE triple;

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

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

        // add outfile padding to the line, if any
        for (int padding = 0; padding < paddingOut; padding++)
            fputc(0x00, outptr);

        // return to the start of the infile scanline
        if (inRow < biIn.biWidth - 1)
            fseek(inptr, biIn.biWidth, SEEK_CUR);

        // this loop should exit at the end of the infile scanline, minus padding

    // skip infile padding, if any, to move to next line of infile
    fseek(inptr, paddingIn, SEEK_CUR);

    // this loop should restart at the beginning of the next infile scanline

    // this loop should exit when all of the infile's scanlines have been read

It seems like I'm copying RGBTRIPLES within each infile scanline correctly, but I can't figure out where I'm going wrong in trying to repeat that process n times.


It looks like you have all the parts, but tried to do too much together.

This is a case where it helps to describe in words what each line of code does. Look at the inner FOR(k) loop. It controls scanning and processing each line. The code with a problem is this:

            // write RGB triple to outfile ***n*** times
            for (int l = 0; l < ***biOut.biWidth***; l++)
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);

            // add outfile padding, if any
            for (int l = 0; l < paddingOut * sizeof(RGBTRIPLE); l++)
                fputc(0x00, outptr);

You want to write each RGB triple out n times, but you write EACH triple times the number of bytes in the line! Next, you add padding to each triple when it should only be added to the end of each line. Kinda makes for a really large file.

Fix the number of times you print each triple and move the add padding code outside of the inner loop and it should work a lot better (don't know if there are any other issues thoguh. ;-) )

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

  • Thanks so much, @cliff! I've made those changes and it's working much better - I think the horizontal resizing is working now and the file size is right ... but i can't get the vertical resizing to work and I can't figure out where I'm going wrong. I've updated my original question & added the revised code. Any thoughts? Thanks in advance! Jun 26 '15 at 17:24
  • if (inRow < biIn.biWidth - 1) Why? You need to reset to beginning of the line without condition while inside the loop. Once outside the loop, you'll need to move forward one line. You might also find ftell() useful.
    – Cliff B
    Jun 26 '15 at 17:38
  • Hey, @cliff! Just wanted to let you know that I figured out the problem. Thanks for the ftell() tip - that helped my gdb bug hunting immensely. I'll leave a hint for folks who stumble on this thread later: remember that fseek() uses bytes ... but not all of your header variables do! Jun 29 '15 at 10:40

You must log in to answer this question.

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