Gamma Correction

The following function shows how to gamma correct a float using RSL. g is the value you wish to correct and f is the gamma value.

float gammacorrect(float g; float f;)
{
	return pow(g, f);
}

To use this inside a shader you would use something like this…

float myValue = 0.5;
float gamma = 2.2;
float myFloat = gammacorrect(myValue, gamma);

The result of which would be 0.5^2.2 = 0.217637641. To correct a colour input, you would need a function like so. c is our colour input and f is the gamma value to use. The major difference here is the use of setcomp in order to operate on each colour channel individually.

color gammacorrect(color c; float f;)
{
	color d;
	setcomp(d, 0, pow(comp(c,0), f));
	setcomp(d, 1, pow(comp(c,1), f));
	setcomp(d, 2, pow(comp(c,2), f));
	return d;
}

Note that Renderman is smart enough to recognise that although we have two functions with the same name called “gammacorrect” – they are returning two different types of data – one being a floating point number the other being a colour. As long as the variable you are passing the data into – in this case “myColour” – is of the correct type, it’ll know what to do with it.

color myValue = 0.5;
float gamma = 2.2;
color myColour = gammacorrect(myValue, gamma);

Leave a Reply

Your email address will not be published. Required fields are marked *