Author Topic: RGB to CieLCH simplification help  (Read 1262 times)


  • Member
  • *****
  • Posts: 1449
  • Assembly is a state of art.
    • RosAsm
RGB to CieLCH simplification help
« on: January 28, 2019, 09:46:20 AM »
Hi Guys, i suceeded to make the necessary functions to compute from RGB to CieLCH and the backwards operation, but, the code is still kind slow due to the mathematical operations involved. So, can someone help me try to simplify the mathematical equations before i try to optimize even further with SSE code etc ?

The steps to transform from RGB to CieLCH are basically these. Convert RGB to XYZ, convert XYZ to LAB and convert Lab to CieLCH. So, the pseudo-code is doing this:

The worst part is the XYZ convertion :dazzled: :dazzled:
1st step - Convert RGB to XYZ
Code: [Select]
TmpRed = Red/255
TmpGreen = Green/255
TmpBlue = Blue/255

Where: Threshold, Offset, Gamma, Slope, WhiteRefX, WhiteRefY and WhiteRefZ are already pre-calculated.

; Gamma must be adjusted to obbey the limits of the threshold. Here those values were already calculated. So, they are "fixed" on this part of the pseudo-code
If TmpRed > Threshold
TmpRed2 = [ (TmpRed+Offset)/(Offset+1) ]^Gamma
TmpRed2 = TmpRed/Slope
TmpRed3 = TmpRed2*100

If TmpGreen > Threshold
TmpGreen2 = [ (TmpGreen+Offset)/(Offset+1) ]^Gamma
TmpGreen2 = TmpGreen/Slope
TmpGreen3 = TmpGreen2*100

If TmpBlue > Threshold
TmpBlue2 = [ (TmpBlue+Offset)/(Offset+1) ]^Gamma
TmpBlue2 = TmpBlue/Slope
TmpBlue3 = TmpBlue2*100

Then multiply the 3 values with the Transpose matrix of ColorSpace

[TmpX, TmpY, TmpZ]  = [TmpRed3, TmpGreen3, TmpBlue3] * [ColorSpaceMatrix]T
ColorSpace Matrix is in the form of:

Matrix1, Matrix2, Matrix3
Matrix4, Matrix5, Matrix6
Matrix7, Matrix8, Matrix9

Whose color space values are (ON this example, only. There are hundreds of matrices that can be used, but i´m posting here for better understanding of what the algorithm is doing) :

0.5767309    0.185554     0.1881851
0.2973769    0.6273491    0.075274
0.0270343    0.0706872    0.9911085

Transposing it will become:
0.5767309    0.2973769     0.0270343
0.185554    0.6273491    0.0706872
0.1881851    0.075274    0.9911085

So, in short, the multiplication of transposed matrix is doing nothing more then:

TmpX  = TmpRed3* Matrix1+ TmpGreen3* Matrix2+ TmpBlue3* Matrix3
TmpY = TmpRed3* Matrix4+ TmpGreen3* Matrix5+ TmpBlue3* Matrix6
TmpZ  = TmpRed3* Matrix7+ TmpGreen3* Matrix8+ TmpBlue3* Matrix9

Then do more math in the resultant matrix multiplication (Again...whiteRefX, WhiteRexY and WhiteRefZ are already pre-calculated. So those values are fixed for this pseudo-code)

TmpX2 = TmpX/WhiteRefX
TmpY2 = TmpY/WhiteRefY
TmpZ2 = TmpZ/WhiteRefZ

If TmpX2 > 1
        TmpX2 = 1

If TmpY2 > 1
        TmpY2 = 1

If TmpZ2 > 1
        TmpZ2 = 1

If TmpX2 > 216/24389
X = TmpX2^(1/3)
X = TmpX2*(841/108) + 16/116   ; Btw. 841/108 is a simplification i made for (24389/27)/116. The original formula goes like this: (TmpX2*(24389/27)+16)/116

; For the 1st pratical result we retrieve the values of X, Y, Z (that are used in RGB to XYZ, Lab to XYZ etc etc)

If TmpY2 > 216/24389
Y = TmpY2^(1/3)
Y = TmpY2*(841/108) + 16/116

If TmpZ2 > 216/24389
Z = TmpZ2^(1/3)
Z = TmpZ2*(841/108) + 16/116

2nd stage - Convert the XYZ to Lab

Code: [Select]
Luminance = Y*116-16
Afactor = (X-Y)*500
Bfactor = (Y-Z)*200

3rd stage - Convert Lab to CieLCH
Code: [Select]
Luminance from Lab is the same for LCH so, no need to do more maths here

Chroma = sqrt(Afactor^2+ Bfactor^2)

Hue = atan2(BFactor/AFactor) ; using atan2 function, but is simple arctan(b/a)

« Last Edit: January 28, 2019, 01:00:34 PM by guga »
Coding in Assembly requires a mix of:
80% of brain, passion, intuition, creativity
10% of programming skills
10% of alcoholic levels in your blood.

My Code Sites: