>> [MUSIC PLAYING]
>> ROB BOWDEN: Hi.
I'm Rob and let's size up this problem.
So we're going to start with copy.c as a template, but we're going to be
making quite a few changes.
>> Now we see we're immediately making a change where we're no longer checking
for our rxc does not equal 3, but now we're checking rc does not equal 4.
Since we also want to include, in addition to the in file and out files
arguments, f which is going to be this factor by which we're scaling.
>> So once we're sure of that, we want to use s scan f to convert the string
argv1 to a float.
And we're going to store that in factor.
This additional character is making sure that we aren't actually entering
something like 1.4 ABC at the command line.
>> Now we're going to create some aliases since RV2 and RV3 aren't
very helpful names.
We're, instead, going to call them in file and out file.
Now we're going to make sure that our factor was actually valid.
So if factor is less than or equal to zero or greater than 100, then as per
the spec, we should reject that factor.
>> When we're sure it's good, now we can open the n file, and we have to make
sure that it was successfully opened.
If it didn't, that will return null.
We're going to open the out file.
And again, we want to check to make sure it's successfully opened.
And if it didn't successfully open, then we also need to be sure to close
the n file which originally successfully opened, or else we have a
memory leak.
>> So now we're going to read in the bitmap file header and bitmap info
header from the n file.
We're going to make sure that the n file was a valid bitmap.
OK.
>> So now we're going to start making some changes.
So because we're going to be changing things, we first want to remember the
old width of the n file.
We want to remember the old padding of the n file using the same calculation
from copy.c.
>> And now we're going to change the bitmap info header.
And so we're multiplying both the width and the height by factor since
that's what we're scaling by.
We're going to determine the new padding of the file by
using the new width.
And we're going to determine the new size of the image using the number of
bytes in a single row which is going to be the number of pixels in that row
times the size of a pixel plus the number of bytes of padding at the end
of that row, and multiplying all that by the number of rows that we have.
So that's the number of bytes we have in our image data.
>> Bf.Bfsize now is going to be the number of bytes in our image beta plus
the size of our headers.
So plus size of bitmap file header and size of bitmap info header.
OK.
So that's it for our headers.
We can write the file head and info header to our out
file, and we're good.
>> Now it's time to start actually writing the pixel
data to the out file.
We're going to declare a buffer of size old width RGB triples, and we're
going to declare a variable called row numb, which is we're going to
initially set equal to negative 1.
We'll see that we're going to be using that in order to keep track of what
row we currently have loaded into this buffer.
OK.
>> So now unlike the standard edition, instead of iterating over at the in
file, we're going to iterate over each row in the out file and figure out
which row in the in file we want to place in this row in the out file.
So iterating over all rows in the out file using the new height, we're first
going to determine the row in the old file we're going to use, which we're
going to do by taking this current row divided by factor.
So that's going to give us the row in the old file that we want.
>> So now if row numb does not equal old y, we're going to have to read the row
that we want into our cur row buffer.
So how are we going to do that?
First, we're going to figure out the position that begins that row in the
original file.
So that position is going to be past all of our headers and
now past old y rows.
>> And so how many bytes are in a single row?
Again, size of RGB triple times old width plus old padding, so that's the
number of bytes in a single row.
And we want to skip past old y rows.
So we're going to f seek and we're using seek set to start from the
beginning of a file.
We're going to f seek to this position in the file, putting us at the
beginning of the row we want to read into our buffer.
>> We're going to set row numb equal to old y.
So now if we loop back and we want to use this same row in our out file,
then we're not going to read it in again unnecessarily.
So really, row numb is just an optimization.
>> Finally, we're going to read into the current row the old width RGB triples
that we want from the original file.
So now cur row contains the pixels from the original file that we want to
write into the out file.
So now, just like above, instead of iterating over the old file, we need
it to iterate over the new files rows.
Well here, instead of iterating over all of the old pixels that in cur row,
we want to iterate over all of the pixels in our new file in this
particular row.
>> Why do we want to do that?
Because we see here that we're not actually necessarily using all of the
pixels in the original file.
Because if we're shrinking, we might actually want to skip the pixels.
And we see that this--
x divided by factor--
closely mirrors up here where we say y divided by factor to figure out that
the old y-th row corresponds to the y-th row in this new file.
>> Now we're going to write all of these pixels from the old row
into our new row.
Once we've done that, we need to just put the padding at the end of our row
and we'll loop back and continue for all of the rows in our new file.
At the end, we need to close our old file, close our new file, and return
zero because everything went fine.
>> My name is Rob and this was Recess.
>> [MUSIC PLAYING]