1
00:00:00,150 --> 00:00:06,150
In this lecture, we're going to create a buffer for an image you may have heard of buffering if you

2
00:00:06,150 --> 00:00:07,050
watched YouTube.

3
00:00:07,380 --> 00:00:13,530
The idea of a buffer is a universal concept in the programming world before we get into the code.

4
00:00:13,740 --> 00:00:15,090
Let's talk about buffers.

5
00:00:15,090 --> 00:00:19,800
In general, buffers are a feature for temporarily storing data.

6
00:00:20,100 --> 00:00:23,540
You can almost think of them as variables, but more powerful.

7
00:00:23,790 --> 00:00:28,290
You may not know it, but you're using a buffer right now to watch this video.

8
00:00:28,740 --> 00:00:32,880
Variables are great for storing numbers, characters or arrays.

9
00:00:33,270 --> 00:00:35,280
Whatever you want to store file data.

10
00:00:35,370 --> 00:00:36,840
We can use a buffer.

11
00:00:37,410 --> 00:00:39,900
Have you ever watched a video on YouTube?

12
00:00:40,080 --> 00:00:41,040
I bet you have.

13
00:00:41,370 --> 00:00:43,710
Video files are extremely large.

14
00:00:44,010 --> 00:00:47,400
The file size of a video can be measured in gigabytes.

15
00:00:47,730 --> 00:00:53,160
Imagine if we live in a world where you would have to download a video before watching it.

16
00:00:53,580 --> 00:00:56,970
You would be waiting an hour to watch a 10 minute cat VIDEO.

17
00:00:57,390 --> 00:00:59,750
Luckily, we don't live in this kind of world.

18
00:01:00,030 --> 00:01:03,990
We can watch videos almost immediately on the video player.

19
00:01:04,019 --> 00:01:06,870
A portion of the timeline is colored in white.

20
00:01:07,290 --> 00:01:10,410
This indicates how much of a file has been downloaded.

21
00:01:10,770 --> 00:01:12,030
How is that possible?

22
00:01:12,300 --> 00:01:15,750
How can we watch a video that hasn't been completely downloaded?

23
00:01:16,080 --> 00:01:17,850
The answer is with buffers.

24
00:01:18,480 --> 00:01:24,630
The buffer acts as a temporary storage for data buffers are mainly used for storing files.

25
00:01:24,900 --> 00:01:31,400
One of the most interesting features of a buffer is being able to read data from it after a buffer has

26
00:01:31,410 --> 00:01:32,370
store to file.

27
00:01:32,520 --> 00:01:34,950
We're allowed to read data from a buffer.

28
00:01:35,310 --> 00:01:40,020
This means we can watch videos without completely downloading an entire video.

29
00:01:41,900 --> 00:01:48,020
We're going to use a buffer to store the image at the moment, our image is a dynamic image.

30
00:01:48,290 --> 00:01:51,170
It needs to be converted back into binary data.

31
00:01:51,440 --> 00:01:53,330
This conversion can take time.

32
00:01:53,630 --> 00:01:56,420
We won't be able to store the data all at once.

33
00:01:56,810 --> 00:02:03,080
Therefore, we're going to need to create a buffer to store the image until the conversion is complete

34
00:02:03,770 --> 00:02:05,600
inside the grayscale function.

35
00:02:06,020 --> 00:02:08,780
Create a mutable variable called buffer.

36
00:02:09,080 --> 00:02:11,330
Its value will be an empty vector.

37
00:02:11,630 --> 00:02:14,390
We can create vectors with the vector macro.

38
00:02:17,090 --> 00:02:21,050
Next, we are going to call the image right to function.

39
00:02:23,630 --> 00:02:29,000
The right to function will initiate the process of converting an image into binary data.

40
00:02:29,300 --> 00:02:35,450
It needs to borrow a vector as its first arguments are going to pass in the buffer variable.

41
00:02:38,070 --> 00:02:44,010
The right to function wants to borrow this variable, however, we can't apply the borrow.

42
00:02:44,040 --> 00:02:47,550
Operator It's because our variable is mutable.

43
00:02:47,850 --> 00:02:52,080
Mutable variables have a different operator for borrowing values.

44
00:02:52,320 --> 00:02:57,660
We can write Ampersand T to allow a function to borrow a value.

45
00:03:00,370 --> 00:03:04,660
The right to function has a second argument for the file type.

46
00:03:05,020 --> 00:03:08,380
We must specify the type of image we're creating.

47
00:03:08,860 --> 00:03:11,500
We have the option of converting the file type.

48
00:03:11,770 --> 00:03:20,110
But for this project or going to output a PNG image at the top of the file, we can import a value called

49
00:03:20,110 --> 00:03:22,900
image image format output PNG.

50
00:03:27,620 --> 00:03:36,380
The PMG value will help the image library, right AP image back in our function pass in the PMG value.

51
00:03:38,840 --> 00:03:39,830
One more step.

52
00:03:40,160 --> 00:03:45,650
The value returned by this function is it results we do not need to store the results.

53
00:03:45,800 --> 00:03:47,540
But Ross does not like that.

54
00:03:48,020 --> 00:03:51,840
We need to handle the result to satisfy the compiler.

55
00:03:51,860 --> 00:03:54,140
We can change the unwrap function.

56
00:03:56,800 --> 00:04:01,060
After writing the image, let's log the following message through the console.

57
00:04:01,300 --> 00:04:02,650
New image written.

58
00:04:05,170 --> 00:04:08,550
Let's give our app one more test by uploading a file.

59
00:04:11,130 --> 00:04:11,580
Great.

60
00:04:11,760 --> 00:04:18,149
We've successfully written the image in the following lecture we will finish out by explaining the image

61
00:04:18,149 --> 00:04:19,019
to the user.

