[MUSIC PLAYING] ZAMYLA CHAN: Let's blow things up with resize. In resize, the user will pass in, via the command line, a bitmap image that they want you to scale by a number that they also pass in, in the command line. How do we do this? Well, first things first, let's break this down into steps. You're going to want to open the infile that they give you, as well as create and open the outfile that you're going to put the resized image in. Then, because you're resizing, and because it's a bitmap, it a header, so you're going to also update the header information for the outfile, and write that in. Then, you're going to read into the scanline of the infile, pixel-by-pixel, resizing horizontally and writing those pixels into the outfile, as specified by the user's scale. You're going to remember to add padding as necessary. More on that later. And then, also resize vertically. OK. So this is going to be a little bit more complicated than Who Done It, but what's similar is that copy.c will, again, prove very useful. Remember that copy.c opens a file, updates the header information for the outfile, then reads into the scanline, pixel-by-pixel, writing every pixel into the output file's scanline. So again, your first step might probably be to cp, copy.c, resize.c into your PSET5 directory. Remember though, before you copy it, to make sure that you understand copy.c very thoroughly. OK. So let's open a file. You know how to do that. I'm going to leave that to you. Next, update the header information for the outfile. Because we have a new bitmap, we have new header info. What's changing here? Well, the file size is going to change because we're going to have more pixels than before. The image size is, thus, also going to change, as is the width and the height. So which variables are those, exactly? Well, if you look into the header information, you see there is biSizeImage, which represents the total size of the image in bytes, including pixels and padding. biWidth is the width of the image in pixels, minus the padding. biHeight is the height of the image in pixels. And so those are contained in the structs BITMAPFILEHEADER and BITMAPINFOHEADER. You can tell which one is which by going to bmp.h and looking at the BITMAPINFOHEADER struct and seeing which variables are listed there. So to update the outfiles header information, you're going to want to change those values of the height and the width. But chances are, you might need some of the infile's header information later, so best to keep track of both. But be very clear with your variable names so that you don't accidentally write the incorrect values in the header for the outfile. So now let's get to reading into the scanline pixel-by-pixel. Again, we're going to turn to our trusty file I/O library, and look at the fread function. fread takes in a pointer to a struct that will contain the bytes that you're reading in, the size of each element that you're reading-- again, sizeof is going to be useful function here, the number of the elements of size, size, that you're reading in, and then finally, the inpointer, the file that you're reading from. So you're taking number elements of size from inpointer, and putting them into data. Now it's time to resize horizontally. if n equals 2, then for each pixel in the infile, we're going to write it twice in the outfile. How do we write files? Well, we have the fwrite function, so we've taken the pointer to the struct that contains the bytes that you're writing from, and then we pass in size, number, and the output, where you're going to be writing that. And then to repeat a process, will be able a simple iterative for loop. But we need to remember to add padding in. The concept of padding is that, well, each pixel is three bites, but the size of each scanline must be a multiple of 4 bytes. So if the number of pixels isn't a multiple of 4, we need to add some padding, which is just zeroes. Now, unlike Who Done It, and unlike copy, then infile image and the outfile file have different have different padding because they're different widths. OK. So perhaps a formula would come in handy here. I'll leave it to you to find it out, but tell you that, to write padding, well, it's just a simple fputc function, passing in the character that you want to write, and then the file pointer that you want to write to. So now that we've resized horizontally, and then used padding, remember that you need to move your file position indicator, because you can't fread into padding. So you want to make sure that your file position indicator in the infile is at the correct point. Because we also want to resize vertically. We can't just stretch it horizontally, because every row needs to be copied n times. Now, there are several different ways to do this. So one, we can use a rewrite method, in that we remember all of the pixels of a given row in an array, and then we write that array as many times as needed. Or there's the recopy method where, after reading in one row in the infile and then writing that into the outfile, adding the padding, we fseek back to the start of the original row, and then repeat the horizontal resizing from there. Regardless of the method, though, you'll want every pixel to be repeated n times, and every row to be repeated n times as well. With that, you'll have bitmap larger than life. My name is Zamyla, and this is CS50.