>> [MUSIC PLAYING]
>> ROB BOWDEN: Hi, I'm Rob, let's try this on for size.
So, we are again going to start with basically a copy of the copy.c, and
make a few changes.
Note immediately we see a change, where instead of checking to see if
argc does not equal 3, we want to check if argc does not equal 4, since
now we're not just taking in an infile and outfile, but we're also taking in
n, which is the amount that we're scaling the original image by.
Once we're sure of that, we can just convert n to an integer using atoi.
>> So, now we're going to create some aliases, just call argv 2 infile, and
argv 3 outfile, because argv 2 and argv 3 aren't very helpful names.
Now we want to check to make sure n is within the bounds we expect, so the
spec specifies that if it's less than or equal to zero, or greater than 100,
then that's an invalid resize factor, and we should warn the user of that.
>> Once we're beyond all that, we can finally open our infile, and we need
to error check to make sure that the opening didn't fail for some reason.
We also need to open our outfile, and again error check to make sure that
didn't fail for some reason.
But be sure if the opening of the outfile failed that we need to close
the infile, which was already opened.
>> So, assuming that didn't fail, then we're-- just as in copy.c--
going to read in the header from the infile.
We're going to make sure it's a valid bitmap, but now we're going to do some
things a bit differently.
So first, we're going to want to remember the original width and height
of the infile, because we're going to be changing it for the outfile.
So now remember that for strange reasons bi.biheight, is negative, and
so the actual height, in positive, we want to take the absolute value.
>> Now, the padding is going to be the same calculation as it was in copy.c,
using the old width, and now we're actually going to change
what's in our header.
So we're going to multiply our width by n, since we're scaling the width by
n, multiply the height by n, since we're scaling by n, and now we have
the calculation for a new padding based on the new width.
>> So now we need to change some other fields in our header.
Bi.biSizeImage is supposed to be the size of all the bytes in the pixels
and padding of the image.
And so the size of a single row of our image is size of RGB triple, the size
of a single pixel, times the number of pixels in a row, plus the padding at
the end of the row.
And then we're going to multiply that by absolute value of our height to get
the total number of bytes in the image data.
bf.bfSize is just the total number of bytes in our image data, so
bi.biSizeImage, plus the size of our headers.
So, adding on the size of bitmap file header, and size of bitmap info
header, OK.
So that's all we need to change in our headers.
Now we'll write the bitmap file header to our outfile, and our bitmap info
header to our outfile, and now we're ready to start going
over the actual pixels.
>> So we want to iterate over the infile's scan lines.
So we want to iterate over oldheight.
This is why we needed to remember the original height, before we change it
and scaled it by n.
Now we're going to read a single row of the infile into a
buffer of size oldwidth.
So here, we're freading size of RGB triple, one pixel, and old width of
them from the infile into our buffer.
And that's going to be an entire row in this array.
So now, we want to iterate n times to print this row to our outfile n times.
And so that's what this loop is doing.
This inner loop is iterating over the row itself, over the array, printing
each pixel in the array n times.
So the zeroth element is being printed n times, the first element is being
printed n times.
And that's sort of how we're going to horizontally scale in the outfile, and
this loop, since we're looping n times, is how we're going to
vertically scale.
>> Down here, we see that we need to add the padding at the end of each row.
That's why this is inside the for loop that's printing out the n rows of just
this one row from the infile.
Then down here, I'm going to skip over the padding in the infile, since once
we're done with one row of the infile, we don't care about
what the padding was.
We just want to get to the second row.
And then we'll loop back and do all this again for the second row.
>> Finally, once we're done with all that, we can close the infile, close
the outfile, and return 0 because we're done.
>> My name is Rob, and this was resize.
>> [MUSIC PLAYING]