-4

i´m having trouble converting RGB to XYZ.

Accordying to this http://easyrgb.com/index.php?X=MATH&H=02#text2 The formula is:

var_R = ( R/255 )
var_G = ( G/255 )
var_B = ( B/255 )

if ( var_R > 0.04045 ) var_R = ( ( var_R + 0.055 ) / 1.055 ) ^ 2.4
else var_R = var_R / 12.92
if ( var_G > 0.04045 ) var_G = ( ( var_G + 0.055 ) / 1.055 ) ^ 2.4
else var_G = var_G / 12.92
if ( var_B > 0.04045 ) var_B = ( ( var_B + 0.055 ) / 1.055 ) ^ 2.4
else var_B = var_B / 12.92

var_R = var_R * 100
var_G = var_G * 100
var_B = var_B * 100

X = var_R * 0.4124 + var_G * 0.3576 + var_B * 0.1805
Y = var_R * 0.2126 + var_G * 0.7152 + var_B * 0.0722
Z = var_R * 0.0193 + var_G * 0.1192 + var_B * 0.9505

The problem is that if i use Red = 7, Green = 7, Blue = 80, the values are wrong. XYZ from the same site computes it as

X = 1.612
Y = 0.776
z = 7.654

But, using the same formula described, the result i´m finding is:

X = 0,60174915012
Y = 0,3723904480
z = 2,3369759394

The values above are the same ones as if i use excel to compute. so, i´m not understanding what i´m doing wrong. If i use the formula provided from the site the values are the ones above, but calculating from their site, the results are different ?

How to properly compute the values of RGBtoXYZ so it can result on the same values as their calculator ? (assuming their calculator are giving the correct values, of course)

Note: Stackoverflow is saying that this is a duplicated question about pow. But it have nothing to do with pow, but a error on the formula above

guga
  • 79
  • 10
  • 2
    Enable compiler warnings and have a C book at hand. – too honest for this site Feb 07 '16 at 17:10
  • Make sure all your variables are declared double and write `R/255 ` as `R/255.0` to force double calculation. – Paul Ogilvie Feb 07 '16 at 17:30
  • 2
    Possible duplicate of [Why is my power operator (^) not working?](http://stackoverflow.com/questions/4843304/why-is-my-power-operator-not-working) – Martin R Feb 07 '16 at 17:36
  • 1
    Note that you could easily find the source of the problem yourself: If an expression does not return the value that you expect, split it into separate simpler statements. Print all intermediate values (or inspect them in the debugger). Compare the values with what you get using (e.g.) a pocket calculator. – Martin R Feb 07 '16 at 17:42
  • Martin, this is not a problem of power operator.. The question have no relation with the mentioned link. About spliting, thanks, but i tried that before. Not working. With other values, the result matches. This is why i´m not understanding what´s wrong with this formula. – guga Feb 07 '16 at 17:47
  • 1
    You are using `^` for exponentiation. That is wrong, and is exactly what the linked-to question is about. – Martin R Feb 07 '16 at 17:47
  • Reading your question again, I notice you haven't even shown any C code, only a copy of the formula in http://easyrgb.com/index.php?X=MATH&H=02#text2, which does not compile in C. So it *might* not be a duplicate, but without seeing your actual code it is unclear what you are asking. – Martin R Feb 07 '16 at 18:00
  • Thanks...I posted the code here: https://gist.github.com/b2kguga/62888d3f69a05912af85 – guga Feb 07 '16 at 19:08
  • 1
    @guga: Please have a look at http://stackoverflow.com/help/how-to-ask and http://stackoverflow.com/help/mcve. A question should have all relevant information *in itself*. And please *tag* the question correctly according to the used programming language, that code does not look like C. – Martin R Feb 07 '16 at 20:43
  • OK, many thanks. The tag means that the question will be directed to the proper programming, i suppose, right ? Can i edit the tags ? And...what if i want the question to be answered in more then one programming language ? I mean, if i need basically the maths of a certain algo to be used either in C or in assembly or others ? – guga Feb 07 '16 at 23:05

1 Answers1

2

In C and C++, ^ is bitwise XOR. You probably meant to use pow().

if ( var_R > 0.04045 ) var_R = pow( ( var_R + 0.055 ) / 1.055, 2.4 )
else var_R = var_R / 12.92
if ( var_G > 0.04045 ) var_G = pow( ( var_G + 0.055 ) / 1.055, 2.4 )
else var_G = var_G / 12.92
if ( var_B > 0.04045 ) var_B = pow( ( var_B + 0.055 ) / 1.055, 2.4 )
else var_B = var_B / 12.92
Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
  • Thanks, but, it is using pow. The problem is that, even powering the number, the results are different. If i insert the values on excel, for example, i´ve got different results. If i use assembly or C and use pow, it also is different from the site. – guga Feb 07 '16 at 17:49
  • @guga where is your implementation? You don't show it – clcto Feb 07 '16 at 17:51
  • On the code, to compute the power of a number. I.e: R@Red = ((R@Red+0.055)/1.055)^2.4, i did "fld R$F_gamma | fld R$F_Offset | fadd R$ecx | fmul R$F_OffsetPlusOne | fyl2x | fld1 | fld ST1 | fprem | f2xm1 | faddp ST1 ST0 | fscale | fxch | fstp ST0" Where on this case, F_Offset = 0,055, F_OffsetPlusOne = 1/1,055, F_gamma = 2,4, and ecx = Red. This is way faster then using the Api pow from msvcrt. – guga Feb 07 '16 at 19:46