// File: kebwi[P]Trifid/src/ImageProcs.cpp
// Function: rgb2xyz(double, double, double, double&, double&, double&)
void rgb2xyz(Float_t r, Float_t g, Float_t b,
			Float_t &x, Float_t &y, Float_t &z)
{
	assert(r >= 0 && r <= 1);
	assert(g >= 0 && g <= 1);
	assert(b >= 0 && b <= 1);
	
	//Good resources:
	//http://www.aim-dtp.net/aim/technology/color-spaces_faq/color-space.faq.txt
	//http://www.brucelindbloom.com/
	
	//----------------------------------------------------------------------
	
	//Taken from http://www.cs.rit.edu/~ncs/color/t_convert.html
	/*
	static const Float_t kM[3][3] =	{
											{ 0.412453, 0.357580, 0.180423 },
											{ 0.212671, 0.715160, 0.072169 },
											{ 0.019334, 0.119193, 0.950227 }
										};
	
	x = r * kM[0][0] + g * kM[0][1] + b * kM[0][2];
	y = r * kM[1][0] + g * kM[1][1] + b * kM[1][2];
	z = r * kM[2][0] + g * kM[2][1] + b * kM[2][2];
	*/
	//----------------------------------------------------------------------
	
	//Taken from http://www.easyrgb.com/math.php
	//and from http://en.wikipedia.org/wiki/SRGB_color_space
	
	/*
	//Convert srgb to linear rgb (if necessary)
	//http://www.sjbrown.co.uk/?article=gamma
	Float_t alpha = .055, gamma = 2.4;
	if (r > 0.04045)
		r = pow((( r + alpha) / (1 + alpha)), gamma);
	else r /= 12.92;
	if (g > 0.04045)
		g = pow((( g + alpha) / (1 + alpha)), gamma);
	else g /= 12.92;
	if (b > 0.04045)
		b = pow((( b + alpha) / (1 + alpha)), gamma);
	else b /= 12.92;
	*/
	//What the hell are these x100 multiplications for?
	/*
	r *= 100;
	g *= 100;
	b *= 100;
	*/
	//Linear rgb to xyz
	//http://www.easyrgb.com/math.php
	//x = r * 0.4124 + g * 0.3576 + b * 0.1805;
	//y = r * 0.2126 + g * 0.7152 + b * 0.0722;
	//z = r * 0.0193 + g * 0.1192 + b * 0.9505;
	
	//More precise values from http://www.mediamacros.com/item/item-1006687658/
	x = r * 0.412453 + g * 0.357580 + b * 0.180423;
	y = r * 0.212671 + g * 0.715160 + b * 0.072169;
	z = r * 0.019334 + g * 0.119193 + b * 0.950227;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movsd %xmm0, -0x8(%rbp)
movsd %xmm1, -0x10(%rbp)
movsd %xmm2, -0x18(%rbp)
movq %rdi, -0x20(%rbp)
movq %rsi, -0x28(%rbp)
movq %rdx, -0x30(%rbp)
movsd -0x8(%rbp), %xmm0
xorl %eax, %eax
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movb %al, -0x31(%rbp)
jb 0x1df69
movsd 0x2b0c2(%rip), %xmm0    # 0x49020
ucomisd -0x8(%rbp), %xmm0
setae %al
movb %al, -0x31(%rbp)
movb -0x31(%rbp), %al
testb $0x1, %al
jne 0x1df72
jmp 0x1df74
jmp 0x1df93
leaq 0x2cda5(%rip), %rdi     # 0x4ad20
leaq 0x2cdaf(%rip), %rsi     # 0x4ad31
movl $0x27, %edx
leaq 0x2cdb2(%rip), %rcx     # 0x4ad40
callq 0x4240
movsd -0x10(%rbp), %xmm0
xorl %eax, %eax
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movb %al, -0x32(%rbp)
jb 0x1dfb9
movsd 0x2b072(%rip), %xmm0    # 0x49020
ucomisd -0x10(%rbp), %xmm0
setae %al
movb %al, -0x32(%rbp)
movb -0x32(%rbp), %al
testb $0x1, %al
jne 0x1dfc2
jmp 0x1dfc4
jmp 0x1dfe3
leaq 0x2cdbe(%rip), %rdi     # 0x4ad89
leaq 0x2cd5f(%rip), %rsi     # 0x4ad31
movl $0x28, %edx
leaq 0x2cd62(%rip), %rcx     # 0x4ad40
callq 0x4240
movsd -0x18(%rbp), %xmm0
xorl %eax, %eax
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movb %al, -0x33(%rbp)
jb 0x1e009
movsd 0x2b022(%rip), %xmm0    # 0x49020
ucomisd -0x18(%rbp), %xmm0
setae %al
movb %al, -0x33(%rbp)
movb -0x33(%rbp), %al
testb $0x1, %al
jne 0x1e012
jmp 0x1e014
jmp 0x1e033
leaq 0x2cf50(%rip), %rdi     # 0x4af6b
leaq 0x2cd0f(%rip), %rsi     # 0x4ad31
movl $0x29, %edx
leaq 0x2cd12(%rip), %rcx     # 0x4ad40
callq 0x4240
movsd -0x8(%rbp), %xmm1
movsd 0x2cb98(%rip), %xmm0    # 0x4abd8
mulsd -0x10(%rbp), %xmm0
movsd 0x2cb83(%rip), %xmm2    # 0x4abd0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd -0x18(%rbp), %xmm0
movsd 0x2cb66(%rip), %xmm2    # 0x4abc8
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq -0x20(%rbp), %rax
movsd %xmm0, (%rax)
movsd -0x8(%rbp), %xmm1
movsd 0x2cb41(%rip), %xmm0    # 0x4abc0
mulsd -0x10(%rbp), %xmm0
movsd 0x2cb2c(%rip), %xmm2    # 0x4abb8
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd -0x18(%rbp), %xmm0
movsd 0x2cb0f(%rip), %xmm2    # 0x4abb0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq -0x28(%rbp), %rax
movsd %xmm0, (%rax)
movsd -0x8(%rbp), %xmm1
movsd 0x2caea(%rip), %xmm0    # 0x4aba8
mulsd -0x10(%rbp), %xmm0
movsd 0x2cad5(%rip), %xmm2    # 0x4aba0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd -0x18(%rbp), %xmm0
movsd 0x2cab8(%rip), %xmm2    # 0x4ab98
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, (%rax)
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)