# How is gamma calculated and applied?

This article provides information on how gamma is implemented on our GenICam and IIDC cameras.

### GenICam Cameras

The Gamma feature in Analog Control manages the gamma correction applied to pixel intensity. The equation used is P' = P^Gamma  where is the input value and P' is the pixel value after gamma correction. Note that the value for Gamma is defined as the power applied to the pixel value, and not the gamma of the display. If a display with a gamma of 2.0 is used, the image gamma can be set to 0.5 to compensate. For sRGB output, set Gamma to 0.4545 (1/2.2).

If linear output data is required, disable gamma by setting GammaEnable to 0.

Gamma and the Pixel Lookup Table (LUT) can both be used at once. Gamma is applied before the Pixel LUT.

### IIDC Cameras

This article is to document knowledge pertaining to the implementation of gamma on the cameras which support the feature. This includes implementation details such as bit depth, IIDC compliance, and LUT usage.

Gamma Approximation

In 8-bit-per-pixel modes (including RGB8) gamma is applied as a sixteen section piece-wise linear approximation using the camera's LUT. The number of entries of the LUT varies with the model of camera.

When using image modes greater than 8 bpp gamma is applied with the same piece-wise linear approximation. However, a LUT with 16+ bit depth is too large and so gamma is not applied using a LUT, but using a piece-wise formula instead.

The end points of each linear section are quite accurate (+/- 1), but between the end points the discrepancy from true gamma values can be quite different. Naturally, the higher the gamma value the less accurate the approximation will be in highly curved regions of the graph.

Below is an example for a gamma setting of 2.5: In this case, with a gamma settings of 2.5, the effect of the piece-wise approximation is visible in dark regions of images. Below is a comparison of the same scene, captured with a FL2-08S2M, of piece-wise and true curves. Note that the piece-wise image has a darker and coarser gradient in dark regions.

##### Linear Gamma g=1 ##### Histogram ##### Piece-wise g=2.5 ##### True g=2.5 Gamma Calculation

To calculate a true curve with gamma value 'g' and n x m bit LUT, where 'n' is the input bit depth and 'm' is the output bit depth, the formula is:
(Output) = ((2^m) -1) x ( ( Input / ((2^n) - 1) ) ^ (1/g) )

This formula will produce the "true" curve for a gamma setting of 'g'. Thus it could be used to generate LUT entries which can then be loaded into the camera's LUT.

Gamma Application

Gamma is applied at the end of a camera's color core. The exact point at which it is applied may vary depending on the video mode.

In RAW and mono modes, gamma is applied last. On some camera models, RAW pixel formats do not apply any gamma.

In color modes, gamma is applied after de-bayering and before RGB2YUV conversion (if applicable).

In 8-bit modes, where the LUT output is 9 bits deep, the pixel value is then reduced to 8 bpp before being transmitted.

LUT Bit depth

Using the convention from Gamma Calculation above, a LUT with n x m bit depth will have a maximum input value of 2^n - 1, and maximum output value of 2^m-1. The LUT will always have 2^n entries. In the case of n > m multiple input values will be converted to the same output value.

All of our camera models which support LUT functionality have a 9 x 9 bit depth, except for the Grasshopper cameras which are 11 x 9. A camera's LUT bit depth is documented in the Technical Reference.

Cameras which support gamma in 16-bit modes apply it from the piece-wise formula (not the LUT) at a bit depth of 16 bits.

IIDC LUT Specification

Before IIDC 1.32, there was no specification for LUT operation. Therefore, we defined our own LUT registers addresses and operation. Cameras which conform to IIDC version 1.32 will conform to the new IIDC LUT spec.

As a result, the register addresses are different between 1.32+ cameras and 1.31- cameras.

Note: IIDC v1.32+ cameras are NOT compatible with FlyCapture1 LUT API methods. To use 1.32 LUT functionality FlyCapture2 is required.