I use OSL texture(...) to perform a texture lookup of an image, succeed, but it always looks whiter than I use the Image Texture Node, Why? What's wrong?
1 Answer
The problem is due to the color correction that is by default applied to the Image Texture (the Color/Non-Color Data option.
A PNG image is stored in sRGB format. This includes a conversion of each color channel to make better use of the available range of 8-bit values for display on screen. The Color/Non-Color Data option of the Image Texture node allows you to convert for this, using the 'RAW' data when set to 'Non-Color Data' or the values converted from sRGB when set to 'Color'.
When you use the 'texture(...)' call in OSL, Cycles returns the unconverted values directly from the image - ie, it is as if you have set the 'Non-Color Data' option in the Image Texture node.
In order to correct for this you need to add some additional code to the OSL shader to include the sRGB to RGB conversion :
shader myshader(
vector Vector = P,
output color Color = color(0,0,0),
output color ColorRGB = color(0,0,0)
)
{
//Replace this path with the path to your image
Color = texture("c:/users/rich/Desktop/map.png", Vector[0], 1.0-Vector[1]);
//Conversion as defined in http://entropymine.com/imageworsener/srgbformula/
for (int c = 0; c<3; c++)
{
if (Color[c] <= 0.0031308)
{
ColorRGB[c] = Color[c] / 12.92;
}
else
{
ColorRGB[c] = pow((Color[c]+0.055)/1.055,2.4);
}
}
}
This is using the formula for conversion as defined at http://entropymine.com/imageworsener/srgbformula/. Also, see Wikipedia sRGB for additional background and information regarding sRGB.
The above OSL code should create a node with a Vector input and two Color outputs (Color and ColorRGB). The Color output produces the color data directly from the texture image (ie, it will still be in sRGB - as if you have selected 'Non-Color Data') while the ColorRGB output provides the converted values (as if you had selected 'Color' on the Image Texture node).