I seem to have a sepia tone that is almost working properly. For some reason a portion of the image turns out to be lime green! Does anyone know what i might be doing wrong? Met
Your values are overflowing and wrapping around.
Your attempt to protect against this with (rgbValues[i + 0]) > 255
has no effect because a byte[]
cannot store values over 255 anyway, so the values overflowed and wrapped as soon as you put them in rgbValues
. You need to clamp them before storing them in the array. C# has a function Math.Min()
that would be excellent for this purpose.
On the other hand, given that you are getting overflow, you probably want to fix that in the first place — clamping will create an "overexposure" effect (because overexposure is clamping), which is probably undesirable. Adjust your coefficients so that you are changing the color but not changing the (perceived) brightness (I don't have a reference for this; sorry).
As an entirely separate problem, as noted by @Yacoder, your first line modifies the inputs the second one uses, and so on, so your calculation will be off. You need to either the three inputs or the three outputs in temporary variables.
You might also want to look into whether System.Drawing.Imaging
has a color matrix image transformation operation, because that's what you're doing by hand here, and the system-provided version will probably be faster. (I don't know C# so I can't comment on that.)