0

I'm trying to resize bmp files in Pset 5 using the re-copy method. However, it seems as though I'm calculating the headers wrongly and I'm also not implementing the for loop where I repeat the horizontal resizing correctly. I would really appreciate any advice on how to overcome these problems!

Here is my code for resize: https://gist.github.com/ehahehah/f9b4073f41b2a56482ce

UPDATE: I added an if statement (line 127) to check if inptr should be repositioned to the beginning of the scanline. https://gist.github.com/ehahehah/0acc422c6dbbd5ffe63f
This is what I got from check50. Previously without adding the if statement only the 1st 2 checks passed check50.
:) resize.c and bmp.h exist
:) resize.c compiles
:) doesn't resize 1x1-pixel BMP when n is 1
:( resizes 1x1-pixel BMP to 2x2 correctly when n is 2
:( resizes 1x1-pixel BMP to 3x3 correctly when n is 3
:( resizes 1x1-pixel BMP to 4x4 correctly when n is 4
:( resizes 1x1-pixel BMP to 5x5 correctly when n is 5
:( resizes 2x2-pixel BMP to 4x4 correctly when n is 2

UPDATE: I haven't seemed to have found what went wrong and I executed:
./resize 4 small.bmp student.bmp
~cs50/pset5/resize 4 small.bmp staff.bmp
~cs50/pset5/peek student.bmp staff.bmp
This is what I got:
From this can I conclude that the problem lies (only) in my header calculations?
Any help would be greatly appreciated!

enter image description here

UPDATE: Having realized that I was using the old headers for my new resized bmp files (with the help of jessicab4!) I made the necessary changes but check50 gave me this (the 3rd check is now a sad face):
:) resize.c and bmp.h exist
:) resize.c compiles
:( doesn't resize 1x1-pixel BMP when n is 1
:( resizes 1x1-pixel BMP to 2x2 correctly when n is 2
:( resizes 1x1-pixel BMP to 3x3 correctly when n is 3
:( resizes 1x1-pixel BMP to 4x4 correctly when n is 4
:( resizes 1x1-pixel BMP to 5x5 correctly when n is 5
:( resizes 2x2-pixel BMP to 4x4 correctly when n is 2

1

From what I see, you're using the same headers on both images. First, you read the headers from the input file, then you created two new structs where you changed and adjusted the values. The problem is that you then don't use those changes because you write the first headers on the output file.

| improve this answer | |
  • This is what I got after making changes to my code :) resize.c and bmp.h exist :) resize.c compiles :( doesn't resize 1x1-pixel BMP when n is 1 :( resizes 1x1-pixel BMP to 2x2 correctly when n is 2 :( resizes 1x1-pixel BMP to 3x3 correctly when n is 3 :( resizes 1x1-pixel BMP to 4x4 correctly when n is 4 :( resizes 1x1-pixel BMP to 5x5 correctly when n is 5 :( resizes 2x2-pixel BMP to 4x4 correctly when n is 2 – user1742 Sep 5 '14 at 5:42
  • I think I finally know where I went wrong. I realized that you have to assign the new structs the value of the old structs because when you create the new structs the members in the structs don't contain any values by default. Anyway, thank you so much for your help! – user1742 Sep 5 '14 at 5:58
0

I'm not sure what's wrong with your header calculations...nothing jumped out at me there, but your resize horizontal code appears to be repeating itself. You have 2 for loops doing the same thing on line 99 and line 111.

Both loops are attempting to repeat the horizontal copy operation n number of times. You only need one of these, and 111 seems like the better choice.

The pseudocode you have, starting at line 99:

line 99// repeat the following n number of times

        // iterate over pixels in scanline

            // temporary storage


            // read RGB triple from infile

            line 111// write RGB triple to outfile n times

as you can see in the bold areas above (99 and 111, respectively) you are copying horizontally once within the for loop at line 111, and then again n more times, at 99. Using a sample n of 4, you'd be taking this:

1 pixel to 16 pixels, because it's copied 4 times in line 111, then that same operation done 4 times because of line 99

Instead, you want code like this:

// iterate over pixels in scanline

            // temporary storage


            // read RGB triple from infile

            line 111// write RGB triple to outfile n times

Which would take this:

1 pixel to 4 pixels

| improve this answer | |
  • But if I only use 1 loop won't that make my code unable to resize vertically? – user1742 Sep 3 '14 at 5:25

You must log in to answer this question.

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