Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
isp:hsl-_rgb [2019/05/27 19:40] – [Detailed algorithm] Igor Yefmov | isp:hsl-_rgb [2021/07/24 13:53] – [Step-by-step algorithm (10x3 bit channel, 30-bit RGB)] Igor Yefmov | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== HSL->RGB ====== | ====== HSL->RGB ====== | ||
- | The text below is based on an Excel workbook attached to this page: {{ :isp:hsl-rgb.xlsm |}}. That Excel file provides a sort of playground where you can try various input data and see the final results, both " | + | The text below is based on a {{ https://docs.google.com/ |
===== Preface ===== | ===== Preface ===== | ||
- | Much is written and is available on the color space conversion from HSL to RGB (for example this [[https:// | + | Much is written and is available on the color space conversion from HSL to RGB (for example this [[https:// |
The below article details the way to perform this conversion without the use of division operations with high enough precision as to satisfy the imaging pipeline quality requirements for the SUB2r camera based on Artix-7 100T FPGA. | The below article details the way to perform this conversion without the use of division operations with high enough precision as to satisfy the imaging pipeline quality requirements for the SUB2r camera based on Artix-7 100T FPGA. | ||
Line 10: | Line 10: | ||
* \(H\) (hue) is a signed '' | * \(H\) (hue) is a signed '' | ||
* \(S\) (saturation) is an '' | * \(S\) (saturation) is an '' | ||
- | * \(L\) (luminosity) is a '' | + | * \(L\) (luminosity) is a '' |
The output of this conversion is an \(RGB\) triplet '' | The output of this conversion is an \(RGB\) triplet '' | ||
Line 20: | Line 20: | ||
\[\frac{1}{x} = \frac{1*N}{x*N}\] | \[\frac{1}{x} = \frac{1*N}{x*N}\] | ||
and choosing \(N\) such that \(x*N\) is a whole power of \(2\) we have an optimization where a division is replaced by a pair of a multiplication followed by a (super cheap!) bit-shift operation by \(Z\) bits: | and choosing \(N\) such that \(x*N\) is a whole power of \(2\) we have an optimization where a division is replaced by a pair of a multiplication followed by a (super cheap!) bit-shift operation by \(Z\) bits: | ||
- | \[\frac{C}{x} = C*\frac{1}{x} = C*\frac{1*N}{x*N} = C*\frac{N}{2^Z} = [(C*N)>>Z]\] | + | \[\frac{C}{x} = C*\frac{1}{x} = C*\frac{1*N}{x*N}\Bigg|_{\{x*N=2^Z\}} = C*\frac{N}{2^Z} = [(C*N) |
The value \(Z\) depends on the needed precision and, of course, the higher the \(Z\) the less precision loss there will be in the end. | The value \(Z\) depends on the needed precision and, of course, the higher the \(Z\) the less precision loss there will be in the end. | ||
Line 28: | Line 28: | ||
The last part, losing the 14 LSB, doesn' | The last part, losing the 14 LSB, doesn' | ||
- | ===== Step-by-step algorithm ===== | ||
- | ==== Precise calculations ==== | + | ===== Precise calculations |
As a refresher this is how [[https:// | As a refresher this is how [[https:// | ||
Line 59: | Line 58: | ||
\[(R, | \[(R, | ||
- | ==== Detailed | + | ===== Step-by-step |
Armed with the above information we can now compile the necessary sequence of calculations and format it into an easy-to-use table: | Armed with the above information we can now compile the necessary sequence of calculations and format it into an easy-to-use table: | ||
- | ^ # ^ name ^ math ^ bits out ^ range ^ C-like code ^ factor ^ | + | ^ # ^ name ^ math excerpt from Wikipedia |
- | | 1 | \(H\) | \[\] | '''' | + | | 1 | \[h\] | \[hue(pixel)\] | '' |
- | | 2 | \(S\) | \[\] | '''' | + | | 2 | \[s\] | \[sat(pixel)\] | |
- | | 3 | \(L\) | \[\] | '''' | + | | 3 | \[l\] | \[luma(pixel)\] | '' |
- | | 4 | \(\) | \[\] | '''' | + | | 4 | \[H\] | \[(H+360^\circ) \bmod 360^\circ\] |
- | | 5 | \(\) | \[\] | '''' | + | H = (h1 > 0 ? h1 : h1 + 4096);</ |
+ | | | \(S\)((not needed for calculations)) | \[\frac{S}{255}\] | | \[0..1\] | | | | ||
+ | | | \(L\)((not needed for calculations)) | \[\frac{S}{511}\] | | \[0..1\] | | | | ||
+ | | 5 | \[L^\prime\] | \[1-|2L-1|\] | | ||
+ | | 6 | \[C\] | \[L^\prime \times S\] | '' | ||
+ | | 7 | \[H^\prime\] | \[\frac{H}{60^\circ}\] | '' | ||
+ | | 8 | \[H^\prime_2\] | \[H^\prime \bmod 2\] | '' | ||
+ | | 9 | \[H^\prime_{2-1}\] | \[H^\prime \bmod 2 - 1\] | '' | ||
+ | | 11 | \[H^\prime_{final}\] | \[1-|H^\prime \bmod 2 - 1|\] | '' | ||
+ | | 12 | \[X\] | \[C \times H^\prime_{final}\] | '' | ||
+ | | 13 | \[(R_1, | ||
+ | / | ||
+ | }else if(Hp < 364*2){ | ||
+ | / | ||
+ | }//...</ | ||
+ | | 14 | \[m\] | \[L - C/2\] | '' | ||
+ | | 15 | \[(R,G,B)\] | \[(R_1+m, | ||
+ | |||
+ | |||
+ | ===== Step-by-step algorithm (10x3 bit channel, 30-bit RGB) ===== | ||
+ | Armed with the above information we can now compile the necessary sequence of calculations and format it into an easy-to-use table: | ||
+ | |||
+ | ^ # ^ name ^ math excerpt from Wikipedia | ||
+ | | 1 | \[H\] | \[hue(pixel)\] | '' | ||
+ | | 2 | \[S\] | \[sat(pixel)\] | | ||
+ | | 3 | \[L\] | \[luma(pixel)\] | '' | ||
+ | | 4 | \[h\] | \[(H+360^\circ) \bmod 360^\circ\] | '' | ||
+ | | 5 | \[L^\prime\] | \[1-|2L-1|\] | '' | ||
+ | | 6 | \[C\] | \[L^\prime \times S\] | '' | ||
+ | | 7 | \[H^\prime\] | \[\frac{H}{60^\circ}\] | '' | ||
+ | | 8 | \[H^\prime_2\] | \[H^\prime \bmod 2\] | | ||
+ | | 9 | \[H^\prime_{2-1}\] | \[H^\prime \bmod 2 - 1\] | '' | ||
+ | | 11 | \[H^\prime_{final}\] | \[1-|H^\prime \bmod 2 - 1|\] | '' | ||
+ | | 12 | \[X\] | \[C \times H^\prime_{final}\] | '' | ||
+ | | 13 | \[(R_1, | ||
+ | / | ||
+ | }else if(Hp < 364*2){ | ||
+ | / | ||
+ | }// | ||
+ | | 14 | \[m\] | \[L - C/2\] | '' | ||
+ | | 15 | \[(R,G,B)\] | \[(R_1+m, | ||