BRIAN YU: The first filter you're going to implement is grayscale.
The grayscale filter should take a color photo
and convert it into grayscale, turning it into black and white.
But how exactly are you going to do that?
Well, let's take a look at a pixel, which as you'll recall,
has a red value, a green value, and a blue value.
If all of those values are zero, the pixel shows up as black.
And remember that if all of the values are the maximum of 255,
then the pixel shows up as white.
The interesting thing to note, though, is as long
as the red, green, and blue values are the same, what you'll get
is a different shade of gray.
If the red, green, and blue values are the same small value,
you'll get a darker shade of gray, something closer
to the black end of the spectrum.
And if the red, green, and blue values are all the same
but are a higher value, then you'll get a lighter shade of gray,
something closer to the white end of the spectrum.
So your task in grayscale is really just, for each pixel,
to make sure that the red, green, and blue values are all the same.
But what value are you going to set them to?
Well, you might imagine that for a lighter color,
you probably want a lighter shade of gray to represent it.
And for a darker color, you probably want a darker shade of gray
to represent that.
So if we have a color that looks a little something like this,
you probably want a lighter shade of gray
to represent it by setting the red, green, and blue values
all to a relatively high value.
But if we have a color that's a little bit darker,
then you probably want a darker shade of gray,
where the red, green, and blue values are all a little bit lower.
And one thing that we can do is just take the red, green, and blue values
and calculate the average pixel value for any of these individual channels
and use that as the new value for red, green, and blue.
What does this look like in practice?
Well, let's take this shade of green, for example, where the red value is
equal to 50, the green value is equal to 190, and the blue value is equal to 90.
If we take these three channel values and take the average of the three,
the average is 110.
So for our new pixel that we're going to convert to grayscale,
we're going to take the red value, set it equal to 110, take the green value,
set it equal to 110, and take the blue value and set it equal to 110 as well.
The result is a shade of gray that approximately matches the brightness
or darkness of the original pixel.
Of course, it's possible that depending on the original color
values, the average value you get might not be a whole number.
In this case, the average is 109.67.
Remember that each of red, green, and blue
need to be integer values in the range from 0 to 255.
So if the average value is not an integer,
you'll want to make sure to round it to the nearest integer
in order to determine what the new value actually should be.
So that's how to take one pixel and convert it to grayscale.
But how are you going to do it for the entire image?
Well, you could imagine looping through the image one row at a time,
starting with the first row, then going to the next row, so on and so forth,
and converting each row to grayscale.
Where for each row, you'd iterate one more time, using yet another loop,
starting with the first pixel in the row, then the second pixel in the row,
so on and so forth.
And for each pixel, what you'll likely want to do
is calculate what the average pixel value
is by taking the red channel value, the green channel
value, and the blue channel value and averaging them together.
Then, once you've calculated the average,
you can set each of the color values--
each of red, green, and blue--
to whatever that average value is--
of course, making sure to round it to the nearest integer.
After you've implemented the grayscale function,
you should be able to try the filter program.
After you make the filter program, you can run ./filter,
passing in -g as a command line argument,
indicating that you'd like to apply the grayscale filter,
passing in an input file as infile.bmp or whatever the file name happens
to be, and then specifying the name of the new file you'd like to produce
as output--
outfile.bmp, for example.
If you do that and pass it an input file in color,
if you've implemented the grayscale function successfully,
after you run the program, you open up the out file,
you should see the same photo, but converted to grayscale.
My name is Brian.
And this was grayscale.