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:contrast [2019/07/14 02:43] Igor Yefmovisp:contrast [2023/09/10 21:53] (current) – [Integer arithmetic] Igor Yefmov
Line 6: Line 6:
 Essentially when the contrast is increased, the pixel's \(L\) value gets "pushed away" from the center of the values' scale, and when the contrast is decreased the \(L\) is being "pushed towards" the center. Essentially when the contrast is increased, the pixel's \(L\) value gets "pushed away" from the center of the values' scale, and when the contrast is decreased the \(L\) is being "pushed towards" the center.
  
 +===== In RGB color space =====
 +
 +Each component's contrast adjustment is based on its value and the adjustment itself is calculated as
 +
 +\[
 +component = (component' - 0.5) * k^2 + 0.5 \\
 +component' \in [0..1.0] \\
 +contrast \in ]0..2.0]%%
 +\]
 +
 +
 +==== Integer arithmetic ====
 +Of course, much like in [[isp::brightness]] case, we'd like to not use floating point arithmetic, so for the 12-bit pixel components and taking into account that the \(contrast\) is in range \([0..2047]\), the math looks like the following:
 +
 +\[
 +\frac{
 +\left(\begin{bmatrix}
 +R  \\
 +G  \\
 +B
 +\end{bmatrix}
 +-
 +\begin{bmatrix}
 +2047 \\
 +2047 \\
 +2047
 +\end{bmatrix} \right) \times contrast^2
 +}{1024*1024} +
 +\begin{bmatrix}
 +2047 \\
 +2047 \\
 +2047
 +\end{bmatrix}
 +\] 
 +
 +===== In HSL color space =====
 For our purposes we are going to adjust the pixel's \(L\) component in the HSL color space, leaving the other two components intact as the Saturation shouldn't be affected by the Contrast and the Hue (at least for now) is also maintaining its invariance during this operation. For our purposes we are going to adjust the pixel's \(L\) component in the HSL color space, leaving the other two components intact as the Saturation shouldn't be affected by the Contrast and the Hue (at least for now) is also maintaining its invariance during this operation.
  
Line 12: Line 48:
 luma = (luma - 50) * contrast^2 + 50 \\ luma = (luma - 50) * contrast^2 + 50 \\
 luma \in [0..100] \\ luma \in [0..100] \\
-contrast \in ]0..1.0]%%+contrast \in ]0..2.0]%%
 \] \]
 <code c++>// pseudo-code <code c++>// pseudo-code
 void contrast(/*array of pixels*/image, double _cst){ void contrast(/*array of pixels*/image, double _cst){
-  for(const & pixel: image){+  for(auto & pixel: image){
     pixel.luma = std::clamp((pixel.luma - 50) * _sct * _sct + 50, 0, 100);     pixel.luma = std::clamp((pixel.luma - 50) * _sct * _sct + 50, 0, 100);
   }   }
 }</code> }</code>
  

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