1 00:00:00,000 --> 00:00:12,240 >> [MUSIC PLAYING] 2 00:00:12,240 --> 00:00:15,870 >> ROB BOWDEN: Hi, I'm Rob, let's try this on for size. 3 00:00:15,870 --> 00:00:21,100 So, we are again going to start with basically a copy of the copy.c, and 4 00:00:21,100 --> 00:00:22,860 make a few changes. 5 00:00:22,860 --> 00:00:26,280 Note immediately we see a change, where instead of checking to see if 6 00:00:26,280 --> 00:00:30,440 argc does not equal 3, we want to check if argc does not equal 4, since 7 00:00:30,440 --> 00:00:34,350 now we're not just taking in an infile and outfile, but we're also taking in 8 00:00:34,350 --> 00:00:38,980 n, which is the amount that we're scaling the original image by. 9 00:00:38,980 --> 00:00:44,340 Once we're sure of that, we can just convert n to an integer using atoi. 10 00:00:44,340 --> 00:00:48,760 >> So, now we're going to create some aliases, just call argv 2 infile, and 11 00:00:48,760 --> 00:00:54,240 argv 3 outfile, because argv 2 and argv 3 aren't very helpful names. 12 00:00:54,240 --> 00:00:58,510 Now we want to check to make sure n is within the bounds we expect, so the 13 00:00:58,510 --> 00:01:02,910 spec specifies that if it's less than or equal to zero, or greater than 100, 14 00:01:02,910 --> 00:01:08,580 then that's an invalid resize factor, and we should warn the user of that. 15 00:01:08,580 --> 00:01:13,090 >> Once we're beyond all that, we can finally open our infile, and we need 16 00:01:13,090 --> 00:01:16,270 to error check to make sure that the opening didn't fail for some reason. 17 00:01:16,270 --> 00:01:19,860 We also need to open our outfile, and again error check to make sure that 18 00:01:19,860 --> 00:01:21,250 didn't fail for some reason. 19 00:01:21,250 --> 00:01:26,270 But be sure if the opening of the outfile failed that we need to close 20 00:01:26,270 --> 00:01:29,040 the infile, which was already opened. 21 00:01:29,040 --> 00:01:33,690 >> So, assuming that didn't fail, then we're-- just as in copy.c-- 22 00:01:33,690 --> 00:01:36,140 going to read in the header from the infile. 23 00:01:36,140 --> 00:01:40,130 We're going to make sure it's a valid bitmap, but now we're going to do some 24 00:01:40,130 --> 00:01:41,620 things a bit differently. 25 00:01:41,620 --> 00:01:44,870 So first, we're going to want to remember the original width and height 26 00:01:44,870 --> 00:01:48,290 of the infile, because we're going to be changing it for the outfile. 27 00:01:48,290 --> 00:01:53,890 So now remember that for strange reasons bi.biheight, is negative, and 28 00:01:53,890 --> 00:01:58,670 so the actual height, in positive, we want to take the absolute value. 29 00:01:58,670 --> 00:02:02,580 >> Now, the padding is going to be the same calculation as it was in copy.c, 30 00:02:02,580 --> 00:02:06,060 using the old width, and now we're actually going to change 31 00:02:06,060 --> 00:02:07,320 what's in our header. 32 00:02:07,320 --> 00:02:11,200 So we're going to multiply our width by n, since we're scaling the width by 33 00:02:11,200 --> 00:02:15,100 n, multiply the height by n, since we're scaling by n, and now we have 34 00:02:15,100 --> 00:02:19,250 the calculation for a new padding based on the new width. 35 00:02:19,250 --> 00:02:21,840 >> So now we need to change some other fields in our header. 36 00:02:21,840 --> 00:02:26,890 Bi.biSizeImage is supposed to be the size of all the bytes in the pixels 37 00:02:26,890 --> 00:02:28,520 and padding of the image. 38 00:02:28,520 --> 00:02:34,190 And so the size of a single row of our image is size of RGB triple, the size 39 00:02:34,190 --> 00:02:39,430 of a single pixel, times the number of pixels in a row, plus the padding at 40 00:02:39,430 --> 00:02:40,910 the end of the row. 41 00:02:40,910 --> 00:02:45,200 And then we're going to multiply that by absolute value of our height to get 42 00:02:45,200 --> 00:02:48,350 the total number of bytes in the image data. 43 00:02:48,350 --> 00:02:53,050 bf.bfSize is just the total number of bytes in our image data, so 44 00:02:53,050 --> 00:02:56,530 bi.biSizeImage, plus the size of our headers. 45 00:02:56,530 --> 00:02:59,850 So, adding on the size of bitmap file header, and size of bitmap info 46 00:02:59,850 --> 00:03:00,800 header, OK. 47 00:03:00,800 --> 00:03:03,170 So that's all we need to change in our headers. 48 00:03:03,170 --> 00:03:07,020 Now we'll write the bitmap file header to our outfile, and our bitmap info 49 00:03:07,020 --> 00:03:09,880 header to our outfile, and now we're ready to start going 50 00:03:09,880 --> 00:03:11,990 over the actual pixels. 51 00:03:11,990 --> 00:03:15,720 >> So we want to iterate over the infile's scan lines. 52 00:03:15,720 --> 00:03:17,730 So we want to iterate over oldheight. 53 00:03:17,730 --> 00:03:20,830 This is why we needed to remember the original height, before we change it 54 00:03:20,830 --> 00:03:23,040 and scaled it by n. 55 00:03:23,040 --> 00:03:27,810 Now we're going to read a single row of the infile into a 56 00:03:27,810 --> 00:03:30,630 buffer of size oldwidth. 57 00:03:30,630 --> 00:03:36,190 So here, we're freading size of RGB triple, one pixel, and old width of 58 00:03:36,190 --> 00:03:39,760 them from the infile into our buffer. 59 00:03:39,760 --> 00:03:43,480 And that's going to be an entire row in this array. 60 00:03:43,480 --> 00:03:50,390 So now, we want to iterate n times to print this row to our outfile n times. 61 00:03:50,390 --> 00:03:52,510 And so that's what this loop is doing. 62 00:03:52,510 --> 00:03:57,910 This inner loop is iterating over the row itself, over the array, printing 63 00:03:57,910 --> 00:04:00,710 each pixel in the array n times. 64 00:04:00,710 --> 00:04:04,510 So the zeroth element is being printed n times, the first element is being 65 00:04:04,510 --> 00:04:05,660 printed n times. 66 00:04:05,660 --> 00:04:10,820 And that's sort of how we're going to horizontally scale in the outfile, and 67 00:04:10,820 --> 00:04:13,390 this loop, since we're looping n times, is how we're going to 68 00:04:13,390 --> 00:04:15,580 vertically scale. 69 00:04:15,580 --> 00:04:19,850 >> Down here, we see that we need to add the padding at the end of each row. 70 00:04:19,850 --> 00:04:25,050 That's why this is inside the for loop that's printing out the n rows of just 71 00:04:25,050 --> 00:04:28,400 this one row from the infile. 72 00:04:28,400 --> 00:04:32,150 Then down here, I'm going to skip over the padding in the infile, since once 73 00:04:32,150 --> 00:04:34,560 we're done with one row of the infile, we don't care about 74 00:04:34,560 --> 00:04:35,290 what the padding was. 75 00:04:35,290 --> 00:04:37,110 We just want to get to the second row. 76 00:04:37,110 --> 00:04:40,870 And then we'll loop back and do all this again for the second row. 77 00:04:40,870 --> 00:04:44,406 >> Finally, once we're done with all that, we can close the infile, close 78 00:04:44,406 --> 00:04:47,430 the outfile, and return 0 because we're done. 79 00:04:47,430 --> 00:04:50,330 >> My name is Rob, and this was resize. 80 00:04:50,330 --> 00:04:54,934 >> [MUSIC PLAYING]