# How is gamma calculated and applied?

Last Revision Date: 5/11/2016

This article provides information on how gamma is implemented on our cameras (excluding Blackfly S). For information on how to implement gamma on Blackfly S, see "How is gamma calculated and applied on Blackfly S 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 certain models, RAW does not have any gamma applied (GS3-U3-23S6C/M-C).

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.