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

#### guga

• Member
• Posts: 1357
• Assembly is a state of art.
##### 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
1st step - Convert RGB to XYZ
Code: [Select]
`TmpRed = Red/255TmpGreen = Green/255TmpBlue = Blue/255Where: 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-codeIf TmpRed > Threshold TmpRed2 = [ (TmpRed+Offset)/(Offset+1) ]^GammaElse TmpRed2 = TmpRed/SlopeEnd_IfTmpRed3 = TmpRed2*100If TmpGreen > Threshold TmpGreen2 = [ (TmpGreen+Offset)/(Offset+1) ]^GammaElse TmpGreen2 = TmpGreen/SlopeEnd_IfTmpGreen3 = TmpGreen2*100If TmpBlue > Threshold TmpBlue2 = [ (TmpBlue+Offset)/(Offset+1) ]^GammaElse TmpBlue2 = TmpBlue/SlopeEnd_IfTmpBlue3 = TmpBlue2*100Then multiply the 3 values with the Transpose matrix of ColorSpace[TmpX, TmpY, TmpZ]  = [TmpRed3, TmpGreen3, TmpBlue3] * [ColorSpaceMatrix]TColorSpace Matrix is in the form of:Matrix1, Matrix2, Matrix3Matrix4, Matrix5, Matrix6Matrix7, Matrix8, Matrix9Whose 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.18818510.2973769    0.6273491    0.0752740.0270343    0.0706872    0.9911085Transposing it will become:0.5767309    0.2973769     0.02703430.185554    0.6273491    0.07068720.1881851    0.075274    0.9911085So, in short, the multiplication of transposed matrix is doing nothing more then:TmpX  = TmpRed3* Matrix1+ TmpGreen3* Matrix2+ TmpBlue3* Matrix3TmpY = TmpRed3* Matrix4+ TmpGreen3* Matrix5+ TmpBlue3* Matrix6TmpZ  = TmpRed3* Matrix7+ TmpGreen3* Matrix8+ TmpBlue3* Matrix9Then 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/WhiteRefXTmpY2 = TmpY/WhiteRefYTmpZ2 = TmpZ/WhiteRefZIf TmpX2 > 1        TmpX2 = 1EndIfIf TmpY2 > 1        TmpY2 = 1EndIfIf TmpZ2 > 1        TmpZ2 = 1EndIfIf TmpX2 > 216/24389 X = TmpX2^(1/3)Else 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)/116EndIf; 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)Else Y = TmpY2*(841/108) + 16/116EndIfIf TmpZ2 > 216/24389 Z = TmpZ2^(1/3)Else Z = TmpZ2*(841/108) + 16/116EndIf`
2nd stage - Convert the XYZ to Lab

Code: [Select]
`Luminance = Y*116-16Afactor = (X-Y)*500Bfactor = (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 hereChroma = 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:
http://rosasm.freeforums.org
http://winasm.tripod.com