User Tools

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
isp:brightness [2023/09/06 09:08] – [In RGB color space] Igor Yefmovisp:brightness [2023/09/10 21:49] (current) – [Calculation reference] Igor Yefmov
Line 6: Line 6:
  
 ===== In RGB color space ===== ===== In RGB color space =====
-In RGB color space the operation is a matrix multiplication of the RGB's vector by a given "brightness" matrixHowever we should make sure that the relative proportion of color channels is preserved so that the colors are not washed out.+The mathematically correct way to do the brightness is to first convert into HSL color space, adjust the L (luma) component, and then convert back into RGB. That gives the correct result but costs way too many transistors and cyclesSo we've got to improvise!
  
-Considering that the luminance is calculated as (see [[isp:luminance]])+==== Algorithm ==== 
 +Here'the algorithm to follow, assuming \(R, G, B \in [0..4095]\) and \(br \in [-1024..+1023]\): 
 +  - calculate luminosity 
 +  - adjust the brightness additively and clamp the value to range \([0..4095]\) 
 +  - figure out the slope for each components based on whether luma is below or above 50% and set chroma components to values that correspond to that 50% luma 
 +  - figure out if the new luma is going to cross the 50% boundary and if so - "flip the slopes" 
 +  - recalculate RGB components
  
-\[lum 0.2126 * R + 0.7152 * G + 0.0722 * B\]+==== Calculation reference ====
  
-Further consider that the brightness is simply "boost" to RGB components, or in other wordsgains. And as such all that is needed is to add the brightness \(br\) proportionally to each channel:+To calculate luminosity we just find the max and min of the triplet and get simple average: 
 +\[L = \frac{min(R, G, B)+max(R, G, B)}{2}\] 
 + 
 +Brightness adjustment is a trivial addition, clamping the value to its proper limits:
  
 \[ \[
-\begin{bmatrix} +L` = L + br \\ 
-R \\ +L` \in [0..4095]
-G \\ +
-+
-\end{bmatrix} +
-+ +
-\begin{bmatrix} +
-0.2126 * br \\ +
-0.7152 * br \+
-0.0722 * br +
-\end{bmatrix}+
 \] \]
  
-Of course when implemented on FPGA the preference is to use integer arithmetic, which can be easily achieved by multiplying the "inconvenient" floating point numbers by \(1024\):+The slope \(k_R\) for the red component calculation depends on whether \(L\) is above or below the middle: 
 +\[ 
 +k_{R} = 
 +\begin{cases} 
 +R / L & \text{if} \; L \leq 2047 \\ 
 +\frac{R - 4095}{L - 4095} & \text{if} \; L > 2047 
 +\end{cases} 
 +\]
  
 +Finding the "middle point" value also depends on whether the \(L\) is above or below the middle:
 \[ \[
-\frac{ +R = 
-\begin{bmatrix+\begin{cases
-\+k_R * 2047 & \text{if} \; L \leq 2047 \\ 
-G \\ +4095 - k_R 2047 & \text{if} \; L > 2047 
-+\end{cases}
-\end{bmatrix} \times 1024 +
-+
-\begin{bmatrix} +
-871 * br \\ +
-2929 br \\ +
-297 * br +
-\end{bmatrix}}{1024}+
 \] \]
  
-N.B. In the above calculations the range for \(R, G, B\) values is \([0..1.0]\), so keep that in mind when using a different scale.+If we are crossing the middle luma boundary as the result of this adjustment - flip the slope: 
 +\[ 
 +k_R = 2 - k_R 
 +\] 
 + 
 +Applying the new \(L`\) to R component and clamping the result is trivial: 
 + 
 +\
 +R` = k_R * (L` - 2047) + R \\ 
 +R` \in [0..4095] 
 +\] 
 + 
 +\(G\) and \(B\) calculations are similar to \(R\). 
 ===== In HSL color space ===== ===== In HSL color space =====
 When using the HSL color space the adjustment is as simple as elementary school's arithmetic operation. Namely - just a simple addition. When using the HSL color space the adjustment is as simple as elementary school's arithmetic operation. Namely - just a simple addition.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also, you acknowledge that you have read and understand our Privacy Policy. If you do not agree, please leave the website.

More information