Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
isp:brightness [2023/09/10 18:30] – [In RGB color space] Igor Yefmov | isp:brightness [2023/09/10 21:01] – [Calculation reference] Igor Yefmov | ||
---|---|---|---|
Line 6: | Line 6: | ||
===== In RGB color space ===== | ===== In RGB color space ===== | ||
- | 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 cycles. So we've got improvise! | + | 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 cycles. So we've got to improvise! |
- | Here's the algorithm to follow, assuming \(R, G, B \in [0..4095] and br \in [-1024..+1023]\): | + | ==== Algorithm ==== |
+ | Here's the algorithm to follow, assuming \(R, G, B \in [0..4095]\) and \(br \in [-1024..+1023]\): | ||
- calculate luminosity | - calculate luminosity | ||
- | - figure out the slope for each components based on whether luma is below or above 50% | ||
- adjust the brightness additively and clamp the value to range \([0..4095]\) | - 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 | - recalculate RGB components | ||
+ | |||
+ | ==== Calculation reference ==== | ||
To calculate luminosity we just find the max and min of the triplet and get a simple average: | To calculate luminosity we just find the max and min of the triplet and get a simple average: | ||
\[L = \frac{min(R, | \[L = \frac{min(R, | ||
+ | |||
+ | Brightness adjustment is a trivial addition, clamping the value to its proper limits: | ||
+ | |||
+ | \[ | ||
+ | L` = L + br \\ | ||
+ | L` \in [0..4095] | ||
+ | \] | ||
The slope \(k_R\) for the red component calculation depends on whether \(L\) is above or below the middle: | The slope \(k_R\) for the red component calculation depends on whether \(L\) is above or below the middle: | ||
Line 26: | Line 37: | ||
\] | \] | ||
- | Similarly find the \(k_G\) and \(k_B\) for green and blue components respectively. | + | Finding the " |
- | + | ||
- | Brightness adjustment | + | |
\[ | \[ | ||
- | L` = L + br \\ | + | R = |
- | L` \in [0..4095] | + | \begin{cases} |
+ | k_R * 2047 & \text{if} \; L \leq 2047 \\ | ||
+ | 255 - k_R * 2047 & \text{if} \; L > 2047 | ||
+ | \end{cases} | ||
\] | \] | ||
- | Apply the new \(L`\) to R component and clamp the result: | + | If we are crossing |
\[ | \[ | ||
- | R` = k_R \times (L` - 2047) + R \\ | + | k_R = 2 - k_R |
- | R` \in [0..4095] | + | |
\] | \] | ||
- | Similarly perform calculations for green and blue components. | + | Applying |
- | ==== Integer arithmetic ==== | + | |
- | Of course when implemented on FPGA the preference is to use integer arithmetic, so for 12-bit RGB components and a 12-bit signed | + | |
\[ | \[ | ||
- | \begin{bmatrix} | + | R` = k_R * (L` - 2047) + R \\ |
- | R \\ | + | R` \in [0..4095] |
- | G \\ | + | |
- | B | + | |
- | \end{bmatrix} | + | |
- | + | + | |
- | \begin{bmatrix} | + | |
- | 871 \\ | + | |
- | 2929 \\ | + | |
- | 297 | + | |
- | \end{bmatrix} \times br \times \frac{1}{1024} \\ | + | |
\] | \] | ||
+ | |||
+ | \(G\) and \(B\) calculations are similar to \(R\). | ||
===== In HSL color space ===== | ===== In HSL color space ===== |