Faster contrast algorithm for a bitmap

前端 未结 2 1481
说谎
说谎 2021-02-02 04:24

I have a tool with trackbar slider controls used to adjust an image\'s brightness, contrast, gamma, etc.

I am trying to get real-time updates to my image while the user

2条回答
  •  天命终不由人
    2021-02-02 04:42

    Depending on the machine you're running this on, your technique could be quite slow. If you're using an ARM system without an FPU, each of those operations would take quite a while. Since you're applying the same operation to every byte, a faster technique would be to create a 256-entry lookup table for the contrast level and then translate each image byte through the table. Your loop would then look like:

    byte contrast_lookup[256];
    double newValue = 0;
    double c = (100.0 + contrast) / 100.0;
    
    c *= c;
    
    for (int i = 0; i < 256; i++)
    {
        newValue = (double)i;
        newValue /= 255.0;
        newValue -= 0.5;
        newValue *= c;
        newValue += 0.5;
        newValue *= 255;
    
        if (newValue < 0)
            newValue = 0;
        if (newValue > 255)
            newValue = 255;
        contrast_lookup[i] = (byte)newValue;
    }
    
    for (int i = 0; i < sourcePixels.Length; i++)
    {
        destPixels[i] = contrast_lookup[sourcePixels[i]];
    }
    

提交回复
热议问题