3
\$\begingroup\$

Currently, I implemented a rudimentary brightness and contrast algorithm that changes brightness and contrast pixel by pixel. This was super inefficient performance wise and the algorithm looked a bit strange.

I heard that I can use a lookup table to get values brightness and contrast for a texture 2D image. How can I do something like this in Unity?

Currently in Unity, I have a prefab in which I load a texture 2D image on top of it. That's the image i want to apply brightness and contrast changes to.

\$\endgroup\$
2
  • \$\begingroup\$ Can you post your current code? On a modern system it would probably be a wash between a look-up table or just doing the math. So if you've profiled it and found that it truly is your brightness/contrast adjustment that's slow, let's see what that looks like. \$\endgroup\$ Jan 30 '16 at 5:50
  • \$\begingroup\$ less than a wash, the math doesn't cost memory latency \$\endgroup\$
    – MickLH
    Feb 29 '16 at 6:27
1
\$\begingroup\$

Something like the following:

public void addContrast(float Contrast)
{
    byte[] contrast_lookup = new byte[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;
    }
}

At this point , for each pixel yuo replace G B R with contrast_lookup(G) contrast_lookup(B) contrast_lookup(R)

\$\endgroup\$
7
  • \$\begingroup\$ Do I need to change each pixel if I'm using a lookup table? Wouldn't that be slow? Also, how do I access the color curves lookup table in Unity? \$\endgroup\$
    – Kala J
    May 3 '15 at 15:15
  • \$\begingroup\$ sorry .. never eart of "color curves lookup table" in Unity \$\endgroup\$ May 3 '15 at 19:33
  • \$\begingroup\$ No problem. Btw, what do you mean by, "At this point , for each pixel yuo replace G B R with contrast_lookup(G) contrast_lookup(B) contrast_lookup(R)"? \$\endgroup\$
    – Kala J
    May 3 '15 at 20:57
  • \$\begingroup\$ And how would you do it for brightness? \$\endgroup\$
    – Kala J
    May 3 '15 at 21:14
  • 8
    \$\begingroup\$ SetPixel() is fairly slow. You'll either want to batch it up using SetPixels(), or better, do this with a Shader as an image effect on the GPU - this is exactly the kind of thing your graphics hardware was made to do. \$\endgroup\$
    – DMGregory
    Jun 2 '15 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.