Cycles node editor - Map gray scale value map to extremes I have a value map that is black and white, but it covers a very small range of that spectrum, having the brightest value at 90% grey, and the darkest somewhere around 80% grey. What I need to do is stretch these values, using nodes, from fully black to fully white, well maintaining their relative brightness. For example, if it spans between 80-90% grey, something at 81% grey would become 10% grey and something 85% grey would be 50% grey, etc.

If possible I would like to do this entirely proceduraly, so that any entered map would work.
The only solution and a major workaround that would work is:


Create a frame change handler that grabs bpy.data.textures and for each texture determines the min and max value in it (by iterating over all pixels). This would be slow. The data should be outputted to some scene custom prop array for example.
Drive a math cycles nodes with those custom prop array items. Everytime you switch texture you would need to re-target to appropriate array item (index). With this the rest of shader is simple.




With only nodes it's impossible because they operate per-sample. You would have to use as many nodes as are pixels in the texture and compare them (you can sample any pixel from texture with mapping vectors).

It would be possible if the min/max values could be encoded into the image (for example as grey values in 2 exact corners). The shader then would crop the texture by some extend and wouldn't use the border of the image with encoded data.



With OSL it's definitely doable but the texture would have to be part of that shader, it wouldn't work with RGB input socket (reason as above). Instead a path to the texture could be an input for such shader.