zestyping / openpixelcontrol

A simple stream protocol for controlling arrays of RGB lights.
http://openpixelcontrol.org/
353 stars 103 forks source link

Where to handle gamma correction? #7

Open longears opened 11 years ago

longears commented 11 years ago

The LEDs we're using need a gamma correction of about 2.2 to match the simulator.

This should probably happen in the TCL driver since it's unique to our hardware. Because we're working with ints, the downside is a loss of color depth -- the brighter values will have 7 effective bits of color instead of 8. Might not be noticeable.

This could be avoided by doing the gamma correction in the client where we might have access to floating point colors, but then the client will need to act differently depending on whether it's talking to the simulator or the real LEDs.

devries commented 11 years ago

I put in a pull request on the TCL server side, which may be a good place to put this. There possible downside would be if the manufacturing changed in such a way that LEDs from the same chipset require a different gamma correction. It might be something to set on a per-server basis? Anyway, I left the gamma factor as something you can set in the code (though not in the client). Maybe we can make a command to adjust the gamma factor?

longears commented 11 years ago

Thanks Chris!

Seems like drivers should assume they're getting un-corrected data by default and they should do the appropriate correction for whatever they're driving.

I can think of two ways to have system messages about this:

Some background info on gamma. Correct me if I'm wrong:

Data tagged as gamma 1.0 means that the pixel value is in "linear space", corresponding to photon intensity. Note that 50% photon intensity looks darker than middle gray to our eyes.

Data tagged as gamma 2.2 means that the pixel values are perceptually uniform, meaning that a gradient from black to white looks balanced to our eyes and a middle gray value actually looks like middle gray.

When we deal with color or pixel values they're almost always in perceptual space, meaning the gamma 2.2 is already baked in. To convert to linear space, e.g. photon intensity, you can apply a gamma of 1/2.2 = 0.4545. Any image operations which are photon-related (e.g. raytracing, simulating camera blur) should happen while the data is in linear space. Image operations which are human-related (e.g. displaying numerical color values, drawing pretty gradients) should happen in perceptual space.

Your computer screen accepts data in perceptual space (gamma of 2.2) and does an internal gamma correction which converts the data to linear space so it knows how many photons it should emit.