# How can I use a lookup table for brightness and contrast?

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.

• 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. Commented Jan 30, 2016 at 5:50
• less than a wash, the math doesn't cost memory latency Commented Feb 29, 2016 at 6:27

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)

• 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? Commented May 3, 2015 at 15:15
• sorry .. never eart of "color curves lookup table" in Unity Commented May 3, 2015 at 19:33
• 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)"? Commented May 3, 2015 at 20:57
• And how would you do it for brightness? Commented May 3, 2015 at 21:14
• 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. Commented Jun 2, 2015 at 17:20