In this lecture, we're going to create a buffer for an image you may have heard of buffering if you
watched YouTube.
The idea of a buffer is a universal concept in the programming world before we get into the code.
Let's talk about buffers.
In general, buffers are a feature for temporarily storing data.
You can almost think of them as variables, but more powerful.
You may not know it, but you're using a buffer right now to watch this video.
Variables are great for storing numbers, characters or arrays.
Whatever you want to store file data.
We can use a buffer.
Have you ever watched a video on YouTube?
I bet you have.
Video files are extremely large.
The file size of a video can be measured in gigabytes.
Imagine if we live in a world where you would have to download a video before watching it.
You would be waiting an hour to watch a 10 minute cat VIDEO.
Luckily, we don't live in this kind of world.
We can watch videos almost immediately on the video player.
A portion of the timeline is colored in white.
This indicates how much of a file has been downloaded.
How is that possible?
How can we watch a video that hasn't been completely downloaded?
The answer is with buffers.
The buffer acts as a temporary storage for data buffers are mainly used for storing files.
One of the most interesting features of a buffer is being able to read data from it after a buffer has
store to file.
We're allowed to read data from a buffer.
This means we can watch videos without completely downloading an entire video.
We're going to use a buffer to store the image at the moment, our image is a dynamic image.
It needs to be converted back into binary data.
This conversion can take time.
We won't be able to store the data all at once.
Therefore, we're going to need to create a buffer to store the image until the conversion is complete
inside the grayscale function.
Create a mutable variable called buffer.
Its value will be an empty vector.
We can create vectors with the vector macro.
Next, we are going to call the image right to function.
The right to function will initiate the process of converting an image into binary data.
It needs to borrow a vector as its first arguments are going to pass in the buffer variable.
The right to function wants to borrow this variable, however, we can't apply the borrow.
Operator It's because our variable is mutable.
Mutable variables have a different operator for borrowing values.
We can write Ampersand T to allow a function to borrow a value.
The right to function has a second argument for the file type.
We must specify the type of image we're creating.
We have the option of converting the file type.
But for this project or going to output a PNG image at the top of the file, we can import a value called
image image format output PNG.
The PMG value will help the image library, right AP image back in our function pass in the PMG value.
One more step.
The value returned by this function is it results we do not need to store the results.
But Ross does not like that.
We need to handle the result to satisfy the compiler.
We can change the unwrap function.
After writing the image, let's log the following message through the console.
New image written.
Let's give our app one more test by uploading a file.
Great.
We've successfully written the image in the following lecture we will finish out by explaining the image
to the user.