Next up is sepia.
In the sepia function, your task is going
to be to implement a filter that takes a bitmap image
and converts it to sepia, giving everything
a reddish brown old-time feel to it.
How are you going to do that?
Well, you're going to take each pixel and convert it to its sepia equivalent.
So what does that look like?
If we take this green pixel, for example, it
has a sepia equivalent wear the red value is 183, the green value is 163,
and the blue value is 127.
But where did these numbers actually come from?
Well, it turns out that there's a sepia formula--
an algorithm that you can use that takes the original red, green,
and blue values and determines what the new red, green, and blue values
should be in the sepia version of that pixel.
And the algorithm looks like this.
We compute the amount of red in the sepia version of the pixel
by taking the original amount of red and multiplying it by 0.393,
taking the original amount of green and multiplying it by 0.769,
and taking the original amount of blue and multiplying it by 0.189.
And there are similar formulas for calculating
how much green should be in the resulting pixel
and how much blue should be in the resulting pixel.
So given a pixel like our green pixel from before,
if we take the original color values-- the original amount of red,
the original amount of green, and the original amount of blue--
then we can compute what the new values should
be just by plugging in all of those original values
into the formula to calculate how much red, green, and blue
there should be in the sepia version of the same pixel.
Of course, when you run this formula, you'll
probably notice that the output you get is not going to be an integer.
And recall that every channel value-- red, green, and blue--
needs to be an integer value.
So what you'll likely want to do is be sure to round
that number to the nearest whole number to make sure
that it is actually an integer.
What other corner cases might you run into, though?
Well, imagine this pixel, where the original red value is 200,
the original green value is 240, and the original blue value is 190.
When you run through this formula, applying the formula
to calculate how much red, green, and blue
there should be in the sepia pixel, what you get is a red value of 299.07,
a green value of 266.36, and a blue value of 207.45.
Of course, we'll want to round these as we did before.
But there is still a problem.
Remember that each of red, green, and blue not only need to be integer values
but they also need to be values in a valid range,
representable by eight bits of memory.
In other words, the minimum value is zero.
And the maximum value is 255.
But in this case, you'll notice that the red and green values both exceed 255.
The red value is to 299 and the green value is to 266.
So in this case, if we ever get channel values that exceed 255,
we'll want to make sure to cap them at 255, setting those values to 255,
to make sure that we can still represent that color value using only eight bits.
So what does this algorithm actually look like?
Well, for each pixel in our two-dimensional array
of pixels that represents our image, we're
going to calculate each new color value using the sepia formula,
using the original amounts of red, green, and blue to figure out
what the new amounts of red, green, and blue should be.
But in particular, we'll want to make sure
that the result we get for each channel value is an integer between 0 and 255,
inclusive.
Once you've written the sepia function, you should be able to test it
by running your filter program by calling ./filer,
passing in the -s command line argument, s for sepia,
specifying what the input file is and what file you'd like to produce
as output.
And if all goes well, you should see your original image converted
to the sepia version of the same image.
My name is Brian.
And this was sepia.