What is wrong with this sepia tone conversion algorithm?

前端 未结 3 841
刺人心
刺人心 2021-02-11 01:41

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

3条回答
  •  我寻月下人不归
    2021-02-11 02:09

    To fix the issue, change the loop like this:

    for (int i = 0; i < rgbValues.Length; i += 4)
    {
        int red = rgbValues[i + 2];
        int green = rgbValues[i + 1];
        int blue = rgbValues[i + 0];
    
        rgbValues[i + 2] = (byte)Math.Min((.393 * red) + (.769 * green) + (.189 * blue), 255.0); // red
        rgbValues[i + 1] = (byte)Math.Min((.349 * red) + (.686 * green) + (.168 * blue), 255.0); // green
        rgbValues[i + 0] = (byte)Math.Min((.272 * red) + (.534 * green) + (.131 * blue), 255.0); // blue
    }
    

    There are arithmetic overflows occurring in your calculations, that's the reason for the wrong colors. An expression of type double is being explicitly cast to byte before it is compared to 255, therefore it will never be greater than 255.

提交回复
热议问题