Today we are going to tackle CS50’s Resize exercise. Before starting this problem, I suggest going over to my last blog post where I explain how copy.c works. This exercise, like Whodunit, builds off the preexisting code in copy.c. To start, we must first make a copy of the input file BITMAPINFOHEADER and BITMAPFILEHEADER and store it in other variables for manipulation. In the following code this variable is represented by bir and bfr respectively. Next we will change the height and width in the info header by n (which is the command line argument).

The next item that we must add is the padding for our copy. Luckily, we got the equation for the padding earlier, so we can simply copy that into another variable. In this example, I am using the variable named outPadding.  The following equations change the file size and image size in the two headers. This is done so that the file headers and the actual file are the same size.

The rest of the code looks very similar to copy.c. This is because it is almost the same code. We first iterate over the scanlines and the begin reading each line and writing to the output file. The difference here is that we have added a little more complexity to the for loops. Here we write the RGBTRIPLE to the scanline n times. This is because, if we have one pixel and the user types in the number 3 at the command line, we now must write three pixels instead of just one. The next addition is to add the padding to the outfile and then change the cursor position. Finally, as always we must make sure to always close the file when we are done.

This has been a very quick tutorial on Resize. I didn’t have a whole lot of time to write this one, so I am sure I have left some stuff out. If something doesn’t make sense or you need clarification, don’t hesitate to contact me. Thanks for reading. Please remember to share this post on your favorite social media site. And as always…Happy Programming.


One comment

  1. if (j < n – 1)
    fseek(inptr, -bi.biWidth * sizeof(RGBTRIPLE), SEEK_CUR);
    I seem not to understand why you subtracted 1 from the if statement…
    Fseek seems to trouble me too


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s