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
Last revisionBoth sides next revision
isp:contrast [2019/05/08 16:57] Igor Yefmovisp:contrast [2023/09/10 21:51] – [In RGB color space] Igor Yefmov
Line 4: Line 4:
 Decreasing the contrast brings the shadows up and highlights down to make them closer to one another. Decreasing the contrast brings the shadows up and highlights down to make them closer to one another.
  
-Essentially when the contrast is increased, the pixel'''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'\(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.
  
-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.+===== In RGB color space =====
  
-Assuming the ''L'' component to be in range ''[0..100]''% the contrast adjustment looks like this:+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  \\ 
 +
 +\end{bmatrix} 
 +
 +\begin{bmatrix} 
 +2047 \\ 
 +2047 \\ 
 +2047 
 +\end{bmatrix} \right) 
 +\begin{bmatrix} 
 +47394 & 0 & 0 \\ 
 +0 & 536358 & 0 \\ 
 +0 & 0 & 5466 
 +\end{bmatrix} \times contrast^2 
 +}{1024*1024*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. 
 + 
 +Assuming the \(L\) component to be in range \([0..100]%%\) the contrast adjustment looks like this: 
 +\[ 
 +luma = (luma - 50) * contrast^2 + 50 \\ 
 +luma \in [0..100] \\ 
 +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 + 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